Class View

java.lang.Object
org.puremvc.java.core.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 an INotification's observer list.
  • Providing a method for broadcasting an INotification.
  • Notifying the IObservers of a given INotification 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 not
    protected void initializeView()
    Initialize the Singleton View instance.
    void notifyObservers​(INotification notification)
    Notify the IObservers for a particular INotification.
    void registerMediator​(IMediator mediator)
    Register an IMediator instance with the View.
    void registerObserver​(java.lang.String notificationName, IObserver observer)
    Register an IObserver to be notified of INotifications with a given name.
    IMediator removeMediator​(java.lang.String mediatorName)
    Remove an IMediator from the View.
    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 an IMediator from the View.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • 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 method View.getInstance()

      Throws:
      java.lang.Error - Error if Singleton instance has already been constructed
  • Method Details

    • getInstance

      public static IView getInstance​(java.util.function.Supplier<IView> factory)

      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

      public void registerObserver​(java.lang.String notificationName, IObserver observer)

      Register an IObserver to be notified of INotifications with a given name.

      Specified by:
      registerObserver in interface IView
      Parameters:
      notificationName - the name of the INotifications to notify this IObserver of
      observer - the IObserver to register
    • notifyObservers

      public void notifyObservers​(INotification notification)

      Notify the IObservers for a particular INotification.

      All previously attached IObservers for this INotification's list are notified and are passed a reference to the INotification in the order in which they were registered.

      Specified by:
      notifyObservers in interface IView
      Parameters:
      notification - the INotification to notify IObservers 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 interface IView
      Parameters:
      notificationName - which observer list to remove from
      notifyContext - remove the observer with this object as its notifyContext
    • registerMediator

      public void registerMediator​(IMediator mediator)

      Register an IMediator instance with the View.

      Registers the IMediator so that it can be retrieved by name, and further interrogates the IMediator for its INotification interests.

      If the IMediator returns any INotification names to be notified about, an Observer is created encapsulating the IMediator instance's handleNotification method and registering it as an Observer for all INotifications the IMediator is interested in.

      Specified by:
      registerMediator in interface IView
      Parameters:
      mediator - a reference to the IMediator instance
    • retrieveMediator

      public IMediator retrieveMediator​(java.lang.String mediatorName)

      Retrieve an IMediator from the View.

      Specified by:
      retrieveMediator in interface IView
      Parameters:
      mediatorName - the name of the IMediator instance to retrieve.
      Returns:
      the IMediator instance previously registered with the given mediatorName.
    • removeMediator

      public IMediator removeMediator​(java.lang.String mediatorName)

      Remove an IMediator from the View.

      Specified by:
      removeMediator in interface IView
      Parameters:
      mediatorName - name of the IMediator instance to be removed.
      Returns:
      the IMediator that was removed from the View
    • hasMediator

      public boolean hasMediator​(java.lang.String mediatorName)

      Check if a Mediator is registered or not

      Specified by:
      hasMediator in interface IView
      Parameters:
      mediatorName - mediator name
      Returns:
      whether a Mediator is registered with the given mediatorName.