Class Controller
- All Implemented Interfaces:
IController
public class Controller extends java.lang.Object implements IController
IController
implementation.
In PureMVC, the Controller
class follows the
'Command and Controller' strategy, and assumes these
responsibilities:
- Remembering which
ICommand
s are intended to handle whichINotifications
. - Registering itself as an
IObserver
with theView
for eachINotification
that it has anICommand
mapping for. - Creating a new instance of the proper
ICommand
to handle a givenINotification
when notified by theView
. - Calling the
ICommand
'sexecute
method, passing in theINotification
.
Your application must register ICommands
with the
Controller.
The simplest way is to subclass Facade
,
and use its initializeController
method to add your
registrations.
- See Also:
View
,Observer
,Notification
,SimpleCommand
,MacroCommand
-
Field Summary
Fields Modifier and Type Field Description protected java.util.concurrent.ConcurrentMap<java.lang.String,java.util.function.Supplier<ICommand>>
commandMap
protected static IController
instance
protected java.lang.String
SINGLETON_MSG
protected IView
view
-
Constructor Summary
Constructors Constructor Description Controller()
Constructor. -
Method Summary
Modifier and Type Method Description void
executeCommand(INotification notification)
If anICommand
has previously been registered to handle a the givenINotification
, then it is executed.static IController
getInstance(java.util.function.Supplier<IController> factory)
Controller
Singleton Factory method.boolean
hasCommand(java.lang.String notificationName)
Check if a Command is registered for a given Notificationvoid
initializeController()
Initialize the SingletonController
instance.void
registerCommand(java.lang.String notificationName, java.util.function.Supplier<ICommand> commandSupplier)
Register a particularICommand
class as the handler for a particularINotification
.void
removeCommand(java.lang.String notificationName)
Remove a previously registeredICommand
toINotification
mapping.
-
Field Details
-
view
-
commandMap
protected java.util.concurrent.ConcurrentMap<java.lang.String,java.util.function.Supplier<ICommand>> commandMap -
instance
-
SINGLETON_MSG
protected final java.lang.String SINGLETON_MSG- See Also:
- Constant Field Values
-
-
Constructor Details
-
Controller
public Controller()Constructor.This
IController
implementation is a Singleton, so you should not call the constructor directly, but instead call the static Singleton Factory methodController.getInstance()
- Throws:
java.lang.Error
- Error if Singleton instance has already been constructed
-
-
Method Details
-
initializeController
public void initializeController()Initialize the Singleton
Controller
instance.Called automatically by the constructor.
Note that if you are using a subclass of
View
in your application, you should also subclassController
and override theinitializeController
method in the following way:// ensure that the Controller is talking to my IView implementation override public function initializeController( ) : void { view = MyView.getInstance(() -> new MyView()); }
-
getInstance
Controller
Singleton Factory method.- Parameters:
factory
- controller supplier function- Returns:
- the Singleton instance of
Controller
-
executeCommand
If an
ICommand
has previously been registered to handle a the givenINotification
, then it is executed.- Specified by:
executeCommand
in interfaceIController
- Parameters:
notification
- anINotification
-
registerCommand
public void registerCommand(java.lang.String notificationName, java.util.function.Supplier<ICommand> commandSupplier)Register a particular
ICommand
class as the handler for a particularINotification
.If an
ICommand
has already been registered to handleINotification
s with this name, it is no longer used, the newICommand
is used instead.The Observer for the new ICommand is only created if this the first time an ICommand has been regisered for this Notification name.
- Specified by:
registerCommand
in interfaceIController
- Parameters:
notificationName
- the name of theINotification
commandSupplier
- theClass
of theICommand
-
removeCommand
public void removeCommand(java.lang.String notificationName)Remove a previously registered
ICommand
toINotification
mapping.- Specified by:
removeCommand
in interfaceIController
- Parameters:
notificationName
- the name of theINotification
to remove theICommand
mapping for
-
hasCommand
public boolean hasCommand(java.lang.String notificationName)Check if a Command is registered for a given Notification
- Specified by:
hasCommand
in interfaceIController
- Parameters:
notificationName
- notification name- Returns:
- whether a Command is currently registered for the given
notificationName
.
-