Name
Observable
Synopsis
This
class is the superclass for classes that want to provide
notifications of state changes to interested
Observer
objects. Register an
Observer
to be notified by passing it to the
addObserver( )
method of an
Observable
, and de-register it by passing it to
the deleteObserver( )
method. You can delete all
observers registered for an Observable
with
deleteObservers( )
, and can find out how many
observers have been added with countObservers( )
.
Note that there is not a method to enumerate the particular
Observer
objects that have been added.
An Observable
subclass should call the protected
method setChanged( )
when its state has changed in
some way. This sets a “state
changed” flag. After an operation or series of
operations that may have caused the state to change, the
Observable
subclass should call
notifyObservers( )
, optionally passing an
arbitrary Object
argument. If the state changed
flag is set, this notifyObservers( )
calls the
update( )
method of each registered
Observer
(in some arbitrary order), passing the
Observable
object, and the optional argument, if
any. Once the update( )
method of each
Observable
has been called,
notifyObservers( )
calls clearChanged(
)
to clear the state changed flag. If
notifyObservers( )
is called when the state
changed flag is not set, it does not do anything. You can use
hasChanged( )
to query the current state of the
changed flag.
The Observable
class and
Observer
interface are not commonly used. Most applications prefer ...
Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.