Class View
- All Implemented Interfaces:
IView
public class View extends java.lang.Object implements IView
A Singleton IView
implementation.
In PureMVC, the View
class assumes these responsibilities:
- Maintain a cache of
IMediator
instances. - Provide methods for registering, retrieving, and removing
IMediators
. - Notifiying
IMediators
when they are registered or removed. - Managing the observer lists for each
INotification
in the application. - Providing a method for attaching
IObservers
to anINotification
's observer list. - Providing a method for broadcasting an
INotification
. - Notifying the
IObservers
of a givenINotification
when it broadcast.
- See Also:
Mediator
,Observer
,Notification
-
Field Summary
Fields Modifier and Type Field Description protected static IView
instance
protected java.util.concurrent.ConcurrentMap<java.lang.String,IMediator>
mediatorMap
protected java.util.concurrent.ConcurrentMap<java.lang.String,java.util.List<IObserver>>
observerMap
protected java.lang.String
SINGLETON_MSG
-
Constructor Summary
Constructors Constructor Description View()
Constructor. -
Method Summary
Modifier and Type Method Description static IView
getInstance(java.util.function.Supplier<IView> factory)
View Singleton Factory method.boolean
hasMediator(java.lang.String mediatorName)
Check if a Mediator is registered or notprotected void
initializeView()
Initialize the Singleton View instance.void
notifyObservers(INotification notification)
Notify theIObservers
for a particularINotification
.void
registerMediator(IMediator mediator)
Register anIMediator
instance with theView
.void
registerObserver(java.lang.String notificationName, IObserver observer)
Register anIObserver
to be notified ofINotifications
with a given name.IMediator
removeMediator(java.lang.String mediatorName)
Remove anIMediator
from theView
.void
removeObserver(java.lang.String notificationName, java.lang.Object notifyContext)
Remove the observer for a given notifyContext from an observer list for a given Notification name.IMediator
retrieveMediator(java.lang.String mediatorName)
Retrieve anIMediator
from theView
.
-
Field Details
-
mediatorMap
-
observerMap
protected java.util.concurrent.ConcurrentMap<java.lang.String,java.util.List<IObserver>> observerMap -
instance
-
SINGLETON_MSG
protected final java.lang.String SINGLETON_MSG- See Also:
- Constant Field Values
-
-
Constructor Details
-
View
public View()Constructor.
This
IView
implementation is a Singleton, so you should not call the constructor directly, but instead call the static Singleton Factory methodView.getInstance()
- Throws:
java.lang.Error
- Error if Singleton instance has already been constructed
-
-
Method Details
-
getInstance
View Singleton Factory method.
- Parameters:
factory
- view supplier function- Returns:
- the Singleton instance of
View
-
initializeView
protected void initializeView()Initialize the Singleton View instance.
Called automatically by the constructor, this is your opportunity to initialize the Singleton instance in your subclass without overriding the constructor.
-
registerObserver
Register an
IObserver
to be notified ofINotifications
with a given name.- Specified by:
registerObserver
in interfaceIView
- Parameters:
notificationName
- the name of theINotifications
to notify thisIObserver
ofobserver
- theIObserver
to register
-
notifyObservers
Notify the
IObservers
for a particularINotification
.All previously attached
IObservers
for thisINotification
's list are notified and are passed a reference to theINotification
in the order in which they were registered.- Specified by:
notifyObservers
in interfaceIView
- Parameters:
notification
- theINotification
to notifyIObservers
of.
-
removeObserver
public void removeObserver(java.lang.String notificationName, java.lang.Object notifyContext)Remove the observer for a given notifyContext from an observer list for a given Notification name.
- Specified by:
removeObserver
in interfaceIView
- Parameters:
notificationName
- which observer list to remove fromnotifyContext
- remove the observer with this object as its notifyContext
-
registerMediator
Register an
IMediator
instance with theView
.Registers the
IMediator
so that it can be retrieved by name, and further interrogates theIMediator
for itsINotification
interests.If the
IMediator
returns anyINotification
names to be notified about, anObserver
is created encapsulating theIMediator
instance'shandleNotification
method and registering it as anObserver
for allINotifications
theIMediator
is interested in.- Specified by:
registerMediator
in interfaceIView
- Parameters:
mediator
- a reference to theIMediator
instance
-
retrieveMediator
Retrieve an
IMediator
from theView
.- Specified by:
retrieveMediator
in interfaceIView
- Parameters:
mediatorName
- the name of theIMediator
instance to retrieve.- Returns:
- the
IMediator
instance previously registered with the givenmediatorName
.
-
removeMediator
Remove an
IMediator
from theView
.- Specified by:
removeMediator
in interfaceIView
- Parameters:
mediatorName
- name of theIMediator
instance to be removed.- Returns:
- the
IMediator
that was removed from theView
-
hasMediator
public boolean hasMediator(java.lang.String mediatorName)Check if a Mediator is registered or not
- Specified by:
hasMediator
in interfaceIView
- Parameters:
mediatorName
- mediator name- Returns:
- whether a Mediator is registered with the given
mediatorName
.
-