public class Services extends Object
in order to access to them from the object instance, such as a Featurable
or a Feature
.
Ensure to add any required services before creating an object with the factory, else it will fail with a
LionEngineException
when calling
Factory.create(com.b3dgs.lionengine.Media)
.
Usage example:
final Services services = new Services(); final Factory factory = services.create(Factory.class); final Camera camera = services.create(Camera.class); final MapTile map = services.create(MapTileGame.class);
Constructor and Description |
---|
Services()
Create a services container.
|
Modifier and Type | Method and Description |
---|---|
<S> S |
add(S service)
Add a service.
|
<S> S |
create(Class<S> service)
Create a service from its type, and automatically
add(Object) it. |
<S> S |
get(Class<S> service)
Get a service from its class.
|
public <S> S create(Class<S> service)
add(Object)
it.
The service instance must provide a public constructor with Services
as single argument, or the public
default constructor. Else, create manually the instance and use add(Object)
on it.
The returned service will allow to keep its reference for an easy final initialization:
final Services services = new Services(); final Factory factory = services.create(Factory.class); // Already added ! final Handler handler = services.create(Handler.class); // Already added ! final Camera camera = services.create(Camera.class); // Already added !
An equivalent code could be:
final Services services = new Services(); final Factory factory = new Factory(services); final Handler handler = new Handler(services); final Camera camera = new Camera(); ... services.add(factory); services.add(handler); services.add(camera);
S
- The service type.service
- The service class.LionEngineException
- If unable to create service or if null
.public <S> S add(S service)
The returned service will allow to add a service and keep its reference for an easy final initialization:
final Services services = new Services(); final Text text = services.add(Graphics.createText(Text.SANS_SERIF, 9, TextStyle.NORMAL));
An equivalent code could be:
final Text text = Graphics.createText(Text.SANS_SERIF, 9, TextStyle.NORMAL); ... services.add(text);
S
- The service type.service
- The service to add.LionEngineException
- If service is null
.public <S> S get(Class<S> service)
The first instance (previously added with add(Object)
or create(Class)
) which fit the required
type is returned.
final Services services = new Services(); services.add(new Camera()); ... final Viewer viewer = services.get(Viewer.class) // Get the camera as viewer
S
- The service type.service
- The service type.LionEngineException
- If service not found or null
.Copyright © 2017 Byron 3D Games Studio. All rights reserved.