Menu
Topics Index
...
`


More Utility Classes >
Siva Nookala - 14 Apr 2016
The Observable class is used to create subclasses that other parts of your program can observe.

When an object subclass undergoes a change, observing classes are notified. Observing classes must implement the Observer interface. Observer interface defines the update() method. The update() method is called when an observer is notified a change in an observed object.

Observable defines the methods shown in the following.

Method Description
void addObserver(Observer obj) Adds obj to the list of objects observing the invoking object.
protected void clearChanged() returns the status of the invoking object to "unchanged."
int countObservers() Returns the number of objects observing the invoking object.
void deleteObserver(Observer obj) Removes obj from the list of objects observing the invoking object.
void deleteObservers() Removes all observers for the invoking object.
boolean hasChanged() Returns true if the invoking object has been modified and false if it has not.
void notifyObservers() Notifies all observers of the invoking object that it has changed by calling update(). A null is passed as the second argument to update().
void notifyObservers(Object obj) Notifies all observers of the invoking object that it has changed by calling update( ). obj is passed as an argument to update().
protected void setChanged() Called when the invoking object has changed.

If you call notifyObservers() with an argument, this object is passed to the observer's update() method as its second parameter. Otherwise, null is passed to update().

The Observer Interface

To observe an observable object, you must implement the Observer interface. This interface defines only the one method shown here:

void update(Observable observOb, Object arg)
observOb is the object being observed, and arg is the value passed by notifyObservers(). The update() method is called when a change in the observed object takes place.

Observer Demo
import java.util.Observable;
import java.util.Observer;

class Watcher implements Observer
{
    public void update(Observable obj, Object arg) // LINE A
    {
        System.out.println("update() called, count is "
                + ((Integer) arg).intValue());
    }
}

class BeingWatched extends Observable
{
    void counter(int period)
    {
        for(; period >= 0; period--)
        {
            setChanged(); // LINE B
            notifyObservers(new Integer(period)); // LINE C
            try
            {
                Thread.sleep(100);
            }
            catch(InterruptedException e)
            {
                System.out.println("Sleep interrupted");
            }
        }
    }
}
public class MainClass
{
    public static void main(String args[])
    {
        BeingWatched observed = new BeingWatched();
        Watcher observing = new Watcher();

        observed.addObserver(observing); // LINE D

        observed.counter(5);
    }
}
OUTPUT

update() called, count is 5
update() called, count is 4
update() called, count is 3
update() called, count is 2
update() called, count is 1
update() called, count is 0

DESCRIPTION

Here in the above program Watcher become the Observer class by implementing Observer interface at LINE A we are overriding the update() in the Observer class. At LINE B we are calling setChanged() to indicate that the current object undergone change and notifying it to the Observer class by calling notifyObservers(Object arg). In the MainClass we created BeingWatched and Watcher objects and at LINE D we are adding an Observer to Observable object.

THINGS TO TRY
  • Check whether the current object undergone change using hasChanged()
  • Count number of Observers we have for current object using countObservers()
  • Delete all Observers for the current object using deleteObservers()

0
Wrong
Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.