The db~variable class is used to create sub classes that other parts of your program can observe. When an object of such a subclass undergoes a change, observing classes are ' notified. Observing classes must implement the Observer interface, which defines the antedate ) method. The update( ) method is called when an observer is notified of a change in an observed object.

Observable defines the methods shown in . An object that is being observed must follow two simple rules. First, if it has changed, it must call set Changed( ). Second, when it is ready to notify observers Of this change, it must call notify Observers( ). This causes the update( ) method in the observing object(s) to be called. Be careful-if the object calls observers() without having previously called set Changed( ), no action will take place. The observed object must call both set Changed( ) and not observers( ) before update will be called.

Notice that notify Observers( ) has two forms: one that takes an argument and one that does not. If you call notify Observers( ) with an argument, this object is passed to the observer's update() method as its second parameter. Otherwise, null is passed to update (). You can use the second parameter for passing any type of object that is appropriate {or you application .

The Observer Interface

To observe an observable object, you must implement the Observer terrace. This interface defines only the one method shown here; • void update execrable observe  Object arg)Here, observe is the object being observed, and arg is the value passed by" notify Observers( ). The update( ) method is called when a change in the observed object takes place.

Share This