public class Observable extends Object
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 and Description |
---|
Observable()
Constructs an observable with no observers.
|
Modifier and Type | Method and Description |
---|---|
void |
addObserver(Observer observer)
Adds an observer to the set of observers for this observable.
|
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 . |
int |
countObservers()
Returns the number of observers of this observable.
|
void |
deleteObserver(Observer observer)
Deletes an observer from the set of observers of this observable.
|
void |
deleteObservers()
Clears the set of observers of this observable.
|
Observer[] |
getObservers()
Gets all the observers of this observable.
|
boolean |
hasChanged()
Gets whether or not this observable has changed.
|
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. |
protected void |
setChanged()
Marks this observable as having been changed.
|
public void addObserver(Observer observer)
If the observer is already added, nothing changes.
The order in which notifications will be delivered to multiple observers is not specified.
observer
- an observer to add.NullPointerException
- if the given observer is null
.protected void clearChanged()
hasChanged()
method will now return false
. This method is
called automatically by the notifyObservers()
method.public int countObservers()
public void deleteObserver(Observer observer)
observer
- the observer to delete.public void deleteObservers()
public Observer[] getObservers()
public boolean hasChanged()
true
if and only if the setChanged()
method has been called more recently than the
clearChanged()
method on this observable, false
otherwise.public void notifyObservers()
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.
Observer.update()
protected void setChanged()
hasChanged()
method will now return true
.