Package ej.observable

Class Observable

  • Direct Known Subclasses:
    KernelObservable

    public class Observable
    extends Object
    This class represents an observable object, or "data" in the model-view paradigm. It can be subclassed to represent an object that the application wants to have observed.

    An observable object can have several observers. An observer may be any object that implements interface Observer. After an observable instance changes, an application calling the observable's notifyObservers() method causes all of its observers to be notified of the change by a call to their Observer.update() method.

    • Constructor Detail

      • Observable

        public Observable()
        Constructs an observable with no observers.
    • Method Detail

      • addObserver

        public void addObserver​(Observer observer)
        Adds an observer to the set of observers for this observable.

        If the observer is already added, nothing changes.

        The order in which notifications will be delivered to multiple observers is not specified.

        Parameters:
        observer - an observer to add.
        Throws:
        NullPointerException - if the given observer is null.
      • deleteObserver

        public void deleteObserver​(Observer observer)
        Deletes an observer from the set of observers of this observable.
        Parameters:
        observer - the observer to delete.
      • deleteObservers

        public void deleteObservers()
        Clears the set of observers of this observable.
      • setChanged

        protected void setChanged()
        Marks this observable as having been changed. The hasChanged() method will now return true.
      • clearChanged

        protected void clearChanged()
        Indicates that this observable has no longer changed, or that it has already notified all of its observers of its most recent change, so that the hasChanged() method will now return false. This method is called automatically by the notifyObservers() method.
      • hasChanged

        public boolean hasChanged()
        Gets whether or not this observable has changed.
        Returns:
        true if and only if the setChanged() method has been called more recently than the clearChanged() method on this observable, false otherwise.
      • countObservers

        public int countObservers()
        Returns the number of observers of this observable.
        Returns:
        the number of observers of this observable.
      • notifyObservers

        public void notifyObservers()
        If this observable has changed, as indicated by the hasChanged() method, then notify all of its observers and then call the clearChanged() method to indicate that this observable has no longer changed.

        Each observer has its update method called.

        See Also:
        Observer.update()
      • getObservers

        public Observer[] getObservers()
        Gets all the observers of this observable.
        Returns:
        an array containing all the observers of this observable.