Class Observable


  • public final class Observable
    extends java.lang.Object
    An observable object.

    This class is similar to java.util.Observable but it has some differences:

    • It has improved concurrency
    • It cannot be subclassed
    • The notifyObservers method does not clear the changed flag
    • Protected methods have been made public
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addObserver​(Observer observer)
      Adds an observer to the set of observers for this object.
      void clearChanged()
      Clears the changed flag.
      void deleteObserver​(Observer observer)
      Deletes an observer from the set of observers of this object.
      void deleteObservers()
      Clears the observer list so that this object no longer has any observers.
      boolean hasChanged()
      Returns true if this object has changed.
      void notifyObservers()
      Notify all of the observers.
      void notifyObservers​(java.lang.Object arg)
      Notify all of the observers.
      void setChanged()
      Sets the changed flag to true.
      • Methods inherited from class java.lang.Object

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

      • Observable

        public Observable()
      • Observable

        public Observable​(Observable observable)
    • Method Detail

      • addObserver

        public void addObserver​(Observer observer)
        Adds an observer to the set of observers for this object.
        Parameters:
        observer - the observer to be added.
      • clearChanged

        public void clearChanged()
        Clears the changed flag.
      • deleteObserver

        public void deleteObserver​(Observer observer)
        Deletes an observer from the set of observers of this object. Passing null to this method will have no effect.
        Parameters:
        observer - the observer to be deleted.
      • deleteObservers

        public void deleteObservers()
        Clears the observer list so that this object no longer has any observers.
      • hasChanged

        public boolean hasChanged()
        Returns true if this object has changed.
      • notifyObservers

        public void notifyObservers()
        Notify all of the observers.

        Each Observer has its update method called with two arguments: this observable object and null. In other words, this method is equivalent to:

        notifyObservers(null)
      • notifyObservers

        public void notifyObservers​(java.lang.Object arg)
        Notify all of the observers.

        Each observer has its update method called with two arguments: this observable object and the arg argument.

      • setChanged

        public void setChanged()
        Sets the changed flag to true.