Package ej.observable
Class Observable
- java.lang.Object
-
- ej.observable.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'snotifyObservers()method causes all of its observers to be notified of the change by a call to theirObserver.update()method.
-
-
Constructor Summary
Constructors Constructor Description Observable()Constructs an observable with no observers.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddObserver(Observer observer)Adds an observer to the set of observers for this observable.protected voidclearChanged()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 thehasChanged()method will now returnfalse.intcountObservers()Returns the number of observers of this observable.voiddeleteObserver(Observer observer)Deletes an observer from the set of observers of this observable.voiddeleteObservers()Clears the set of observers of this observable.Observer[]getObservers()Gets all the observers of this observable.booleanhasChanged()Gets whether or not this observable has changed.voidnotifyObservers()If this observable has changed, as indicated by thehasChanged()method, then notify all of its observers and then call theclearChanged()method to indicate that this observable has no longer changed.protected voidsetChanged()Marks this observable as having been changed.
-
-
-
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 isnull.
-
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. ThehasChanged()method will now returntrue.
-
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 thehasChanged()method will now returnfalse. This method is called automatically by thenotifyObservers()method.
-
hasChanged
public boolean hasChanged()
Gets whether or not this observable has changed.- Returns:
trueif and only if thesetChanged()method has been called more recently than theclearChanged()method on this observable,falseotherwise.
-
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 thehasChanged()method, then notify all of its observers and then call theclearChanged()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.
-
-