Each instance of class Observable
maintains a set of "observers" that are notified
whenever the Observable
object changes in some significant way. An observer
may be any object that implements interface Observer
(§21.8).
Note that this notification mechanism is has nothing to do with threads (§20.20) and is completely separate from the wait
and notify
mechanism of class Object
(§20.1).
public classObservable
{ public voidaddObserver
(Observer o); public voiddeleteObserver
(Observer o); public voiddeleteObservers
(); public intcountObservers
(); public voidnotifyObservers
(); public voidnotifyObservers
(Object arg); protected voidsetChanged
(); protected voidclearChanged
(); public booleanhasChanged
(); }
When an observable object is newly created, its set of observers is empty.
Two observers are considered the same if and only if the equals
method (§20.1.3) returns true
for them.
21.7.1 public void addObserver(Observer o)
The observer o
is added to this Observable
object's set of observers, provided
that it is not the same as some observer already in the set.
21.7.2 public void deleteObserver(Observer o)
The observer o
is removed from this Observable
object's set of observers.
21.7.3 public void deleteObservers()
All observers are removed from this Observable
object's set of observers.
21.7.4 public int countObservers()
The number of observers in this Observable
object's set of observers is returned.
21.7.5 public void notifyObservers()
If this Observable
object has been marked as changed, this method causes all
observers to be notified with null
as the second argument; in other words, this
method is equivalent to:
notifyObservers(null)
21.7.6 public void notifyObservers(Object arg)
If this Observable
object has been marked as changed (§21.7.9), this method
causes all observers to be notified with arg
as the second argument. An observer
is notified by calling its update
method (§21.8.1) on two arguments: this
Observable
object and arg
. The mark on this object is then cleared (§21.7.8).
21.7.7 protected void setChanged()
This Observable
object is marked as having been changed; the hasChanged
method will now return true
.
21.7.8 protected void clearChanged()
This Observable
object is marked as not having been changed; the hasChanged
method will now return false
.
21.7.9 public boolean hasChanged()
The result is true if and only if the setChanged
method has been called for this
Observable
object more recently than either the clearChanged
method or the
notifyObservers
method.