AGENT
- PARAMS
- public class GuiceAgentFactory<AGENT extends IAgent,PARAMS extends IAgentParameters> extends AbstractGuiceAgentFactory implements IAgentFactory<AGENT,PARAMS>
IAgent
factory that instantiates the agent according to the bindigs that are found inside
the GuiceAgentModule
, which is provided during the construction.
Additionally to the classic Injector.getInstance(Class)
method, the class offers a place where to
slip runtime dependencies. I.e., if you are unable to specify some dependencies during class coding, you need
to create Provider
s for the runtime dependency (such as desired IAgentId
for the IAgent
).
These dependencies are injected into the module via the method configureModule(IAgentParameters)
that is called before the new instance is created.
Additionally, the factory method newAgent(IAgentParameters)
clears the agent scope
that is defined by the GuiceAgentModule
.
NOTE: if you are going to extend the implementation, than the only place that should suffice you for hacking is
configureModule(IAgentParameters)
where you slip runtime dependencies that the base GuiceAgentFactory
is unaware of, taking these dependencies from your custom IAgentParameters
implementation.
NOTE: you might not need to override the configureModule(IAgentParameters)
as the
module configuration might be also done inside GuiceAgentModule.prepareNewAgent(IAgentParameters)
which
is implicitly called from the configureModule(IAgentParameters)
.
THREAD-SAFE
Constructor and Description |
---|
GuiceAgentFactory(GuiceAgentModule module)
Creates a Guice-based factory that will use
Injector created using the 'module'. |
Modifier and Type | Method and Description |
---|---|
protected void |
configureModule(PARAMS agentParameters)
Called from within the
newAgent(IAgentParameters) to configure the AbstractGuiceAgentFactory.getAgentModule()
with variables from 'agentParams'. |
AGENT |
newAgent(PARAMS agentParameters)
Creates a new instance of the
IAgent interface that is cast to AGENT parameter. |
getAgentModule, getInjector, setAgentModule
public GuiceAgentFactory(GuiceAgentModule module)
Injector
created using the 'module'.
The module MUST specify bindings for the IAgent
interface as that is what's going to be
instantiated using the injector from AbstractGuiceAgentFactory.getInjector()
.
module
- module that configures bindings between classes, may be null (specify module later using AbstractGuiceAgentFactory.setAgentModule(GuiceAgentModule)
)protected void configureModule(PARAMS agentParameters)
newAgent(IAgentParameters)
to configure the AbstractGuiceAgentFactory.getAgentModule()
with variables from 'agentParams'.
Just calls GuiceAgentModule.prepareNewAgent(IAgentParameters)
.
NOTE: You will probably need to override this method in subclasses. If you do - do not forget to call super.configureModule(agentParameters) first! So other runtime-dependencies can be set too.
agentParameters
- public AGENT newAgent(PARAMS agentParameters) throws cz.cuni.amis.utils.exception.PogamutException
IAgent
interface that is cast to AGENT parameter.
Firstly, it calls configureModule(IAgentParameters)
to configure run-time
dependencies of the module and prepare it for the new agent instance, secondly,
it instantiates a new IAgent
instance.
NOTE: that the GuiceAgentFactory
must be correctly instantiated, i.e.,
the module passed into the constructor must bind IAgent
interface to the
AGENT (or descendant) class, otherwise you may experience ClassCastException
.
newAgent
in interface IAgentFactory<AGENT extends IAgent,PARAMS extends IAgentParameters>
agentParameters
- cz.cuni.amis.utils.exception.PogamutException
Copyright © 2012 AMIS research group, Faculty of Mathematics and Physics, Charles University in Prague, Czech Republic. All Rights Reserved.