Java in a Nutshell, 5th Edition [Electronic resources]

نسخه متنی -صفحه : 1191/ 739
نمايش فراداده

Observablejava.util

Java 1.0

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 the event-based notification model defined by the JavaBeans component framework and by the EventObject class and EventListener interface of this package.

public class

Observable { // Public Constructors public

Observable ( ); // Public Instance Methods public void

addObserver (Observer

o ); synchronized public int

countObservers ( ); synchronized public void

deleteObserver (Observer

o ); synchronized public void

deleteObservers ( ); synchronized public boolean

hasChanged ( ); synchronized public void

notifyObservers ( ); public void

notifyObservers (Object

arg ); // Protected Instance Methods protected void

clearChanged ( ); synchronized protected void

setChanged ( ); synchronized }

Passed To

Observer.update( )