Class Observer

java.lang.Object
org.puremvc.java.patterns.observer.Observer
All Implemented Interfaces:
IObserver

public class Observer
extends java.lang.Object
implements IObserver

A base IObserver implementation.

An Observer is an object that encapsulates information about an interested object with a method that should be called when a particular INotification is broadcast.

In PureMVC, the Observer class assumes these responsibilities:

  • Encapsulate the notification (callback) method of the interested object.
  • Encapsulate the notification context (this) of the interested object.
  • Provide methods for setting the notification method and context.
  • Provide a method for notifying the interested object.
See Also:
View, Notification
  • Constructor Summary

    Constructors 
    Constructor Description
    Observer​(java.util.function.Consumer<INotification> notifyMethod, java.lang.Object notifyContext)
    Constructor.
  • Method Summary

    Modifier and Type Method Description
    boolean compareNotifyContext​(java.lang.Object object)
    Compare an object to the notification context.
    protected java.lang.Object getNotifyContext()
    Get the notification context.
    protected java.util.function.Consumer<INotification> getNotifyMethod()
    Get the notification method.
    void notifyObserver​(INotification notification)
    Notify the interested object.
    void setNotifyContext​(java.lang.Object notifyContext)
    Set the notification context.
    void setNotifyMethod​(java.util.function.Consumer<INotification> notifyMethod)
    Set the notification method.

    Methods inherited from class java.lang.Object

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

    • Observer

      public Observer​(java.util.function.Consumer<INotification> notifyMethod, java.lang.Object notifyContext)

      Constructor.

      The notification method on the interested object should take one parameter of type INotification

      Parameters:
      notifyMethod - the notification method of the interested object
      notifyContext - the notification context of the interested object
  • Method Details

    • compareNotifyContext

      public boolean compareNotifyContext​(java.lang.Object object)

      Compare an object to the notification context.

      Specified by:
      compareNotifyContext in interface IObserver
      Parameters:
      object - the object to compare
      Returns:
      boolean indicating if the object and the notification context are the same
    • notifyObserver

      public void notifyObserver​(INotification notification)

      Notify the interested object.

      Specified by:
      notifyObserver in interface IObserver
      Parameters:
      notification - the INotification to pass to the interested object's notification method.
    • getNotifyContext

      protected java.lang.Object getNotifyContext()

      Get the notification context.

      Returns:
      the notification context (this) of the interested object.
    • setNotifyContext

      public void setNotifyContext​(java.lang.Object notifyContext)

      Set the notification context.

      Specified by:
      setNotifyContext in interface IObserver
      Parameters:
      notifyContext - the notification context (this) of the interested object.
    • getNotifyMethod

      protected java.util.function.Consumer<INotification> getNotifyMethod()

      Get the notification method.

      Returns:
      the notification (callback) consumer function of the interested object.
    • setNotifyMethod

      public void setNotifyMethod​(java.util.function.Consumer<INotification> notifyMethod)

      Set the notification method.

      The notification method should take one parameter of type INotification.

      Specified by:
      setNotifyMethod in interface IObserver
      Parameters:
      notifyMethod - the notification (callback) consumer function of the interested object.