public class Observable
extends java.lang.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 object.
|
protected void |
clearChanged()
Indicates that this object 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 object.
|
void |
deleteObserver(Observer observer)
Deletes an observer from the set of observers of this object.
|
Observer[] |
getObservers()
Gets all observers of this observable object.
|
boolean |
hasChanged()
Gets whether or not this object has changed.
|
void |
notifyObservers()
If this object has changed, as indicated by the
hasChanged() method, then notify all of its observers
and then call the clearChanged() method to indicate that this object has no longer changed. |
protected void |
setChanged()
Marks this observable object as having been changed.
|
public void addObserver(Observer observer) throws java.lang.NullPointerException
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 be added.java.lang.NullPointerException
- if the parameter is null
.public void deleteObserver(Observer observer)
If the given observer is null
nothing changes.
observer
- the observer to be deleted.protected void setChanged()
hasChanged()
method will now return
true
.protected void clearChanged()
hasChanged()
method will now return false
. This method is
called automatically by the notifyObservers()
method.public boolean hasChanged()
true
if and only if the setChanged()
method has been called more recently than the
clearChanged()
method on this object, false
otherwise.public int countObservers()
public void notifyObservers()
hasChanged()
method, then notify all of its observers
and then call the clearChanged()
method to indicate that this object has no longer changed.
Each observer has its update method called.
Observer.update()
public Observer[] getObservers()