java.lang.Object | |
↳ | sofia.util.Observable |
Known Direct Subclasses |
A base class for classes that want to send out change notifications using the "observer" pattern.
If the variable observable
is a reference to
an object that extends the Observable
class, then any other object
can observe changes to it by calling addObserver(Object)
:
observable.addObserver(x);
In this case, when a change notification occurs, the system will call, on
the object x
, a method named changeWasObserved
, whose first
parameter is the same type (or a superclass) as observable
. See the
documentation for the notifyObservers(Object)
method to see how
the changeWasObserved
callback can take additional parameters as
well.
To use a different method name than changeWasObserved
, pass its name
as the second parameter to addObserver(Object, String)
:
observable.addObserver(x, "methodToCall");
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Observable()
Initializes the object with an empty set of observers.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
synchronized void |
addObserver(Object observer, String method)
Adds the specified object | ||||||||||
synchronized void |
addObserver(Object observer)
Adds the specified object | ||||||||||
synchronized void |
clearObservers()
Removes all observers from the receiver.
| ||||||||||
void |
notifyObservers(Object... arguments)
Notifies all observers of the receiver that a change has occurred affecting the state of the object. | ||||||||||
synchronized void |
removeObserver(Object observer, String method)
Removes the specified object's named method from the set of observers
for the receiver.
| ||||||||||
synchronized void |
removeObserver(Object observer)
Removes the specified object's
changeWasObserved method from the
set of observers for the receiver. |
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
|
Initializes the object with an empty set of observers.
Adds the specified object observer
to the set of observers for
this object, but using a method name different from the default. When
an instance of this observable object calls #notifyObservers(),
the method with the specified name will be called on the observer.
The method specified here must take a single argument that is the same
type (or a superclass) of this observable object. You can also have
other overloads of this method that take additional parameters after the
observable parameter; these match the parameters that the observable
passes when it calls notifyObservers(Object)
.
observer | the object that will observe changes to this object |
---|---|
method | the name of the method that will be called on the observer |
Adds the specified object observer
to the set of observers for
this object. When an instance of this observable object calls
#notifyObservers(), the changeWasObserved
method will be
called on the observer.
The changeWasObserved
method on the observer must take a single
argument that is the same type (or a superclass) of this observable
object. You can also have other overloads of changeWasObserved
that take additional parameters after the observable parameter; these
match the parameters that the observable passes when it calls
notifyObservers(Object)
.
observer | the object that will observe changes to this object |
---|
Removes all observers from the receiver.
Notifies all observers of the receiver that a change has occurred affecting the state of the object. Typically, this method is called from inside a setter method or some other method that performs a computation or an action on an observable object.
This method can take an argument list of your choosing. These arguments
are passed directly to the changeWasObserved
method, after the
observable object itself, on the observer object. So, for example, if
your subclass of Observable
is called MyModel
, and you
call notifyObservers
from within it like this:
notifyObservers("hello", 5, 9.3);
Then the observer would be searched for a changeWasObserved
method that has its first parameter of type MyModel
(or a
superclass) followed by parameter types compatible with those in the
argument list above; for example:
public void changeWasObserved(MyModel model, String str, int x, double y)
If no such method exists, then an attempt is made to call one that takes only the observable object as a parameter.
public void changeWasObserved(MyModel model)
The order that the observers are called is undefined. User code should not be written that depends on some observers being called before or after others.
Removes the specified object's named method from the set of observers
for the receiver. In other words, after this method is called, the
method with the name given in the method
argument will no longer
be called when this object notifies observers of changes.
observer | the observer whose method should be removed from the set of observers |
---|---|
method | the method that should be removed from the set of observers |
Removes the specified object's changeWasObserved
method from the
set of observers for the receiver. In other words, after this method is
called, the changeWasObserved
method will no longer be called
when this object notifies observers of changes.
observer | the observer whose changeWasObserved method
should be removed from the set of observers
|
---|