public class Services extends Object
Factory
, Handler
,
Camera
, Cursor
... in order to access to them
from the object instance (created by a Factory
in constructor ObjectGame.ObjectGame(Setup, Services)
).
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:
private final Services services = new Services(); private final Factory factory = services.create(Factory.class); private final Camera camera = services.create(Camera.class); private final MapTile map = services.create(MapTileGame.class);
Factory
,
ObjectGame
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:
private final Services services = new Services(); private final Factory factory = services.create(Factory.class); // Already added ! private final Camera camera = services.create(Camera.class); // Already added ! private final Handler handler = services.create(Handler.class); // Already added !
An equivalent code could be:
private final Services services = new Services(); private final Factory factory = new Factory(services); private final Camera camera = new Camera(); private final Handler handler = new Handler(); ... services.add(factory); services.add(camera); services.add(handler);
S
- The service type.service
- The service class.LionEngineException
- If unable to create service or if null
.public <S> S add(S service)
Featurable
, all its features will be added
Featurable.getFeatures()
) with add(Object)
(and so on).
The returned service will allow to add a service and keep its reference for an easy final initialization:
private final Services services = new Services(); private final Text text = services.add(Graphics.createText(Text.SANS_SERIF, 9, TextStyle.NORMAL));
An equivalent code could be:
private 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.
Services services = new Services(); services.add(new Camera()); ... 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 © 2016 Byron 3D Games Studio. All rights reserved.