Observable Class In Java

Introduction

The Observable class is basically used to create subclasses that other parts of the program can observe. When an object of a sub class undergoes a change, observing classes are notified. Observing classes implements the Observer interface, which defines the update() method.

Rules to follow the Observable class

When an observer is notified of a change in an observed object, then the update() method is called.

An object that is being observed and must follow the two basic rules.

  1.  If it has changed, it must call setChanged().
  2. When it is ready to notify observers of this change, it must call notifyObservers(). This causes the update() method in the observing object to be called.

notifyObservers() has two forms

One takes an argument and the other one does not take an argument. If user calls 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 user can use the second parameter for passing any type of object that is appropriate for the application.

Methods of Observable class

void addObserver(Observer obj)

Add to the list of objects observing the invoking object.

protected void clearChanged()

Method returns the status of the invoking object to “unchanged”

int countObservers()

Returns the number of objects observing the invoking object.

void deleteObserver(Observer obj)

Remove obj from the list of objects observing the invoking object.

void notifyObservers(Object obj)

Notifies all the observers that it has changed by calling update(). obj is passed as an argument to update().

Observer Interface

It’s must to implement an observable interface to observe an observable object. This interface defines only one method as below:

void update(Observable ob, Object args)

Where ob is the object being observed, and args is the value passed by notifyObservers(). The update() is called when a change in the observed object takes place.

The following code illustrates the example of Observer interface.

import java.util.*;
class Watcher implements Observer {
    public void update(Observable obj, Object arg) {
        System.out.println("Update() called , count is" + ((Integer) arg).intValue());
    }
}
class BeingWatched extends Observable {
    void counter(int period) {
        for (; period >= 0; period--) {
            setChanged();
            notifyObservers(new Integer(period));
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                System.out.println("Sleep interrupted");
            }
        }
    }
}
class ObserverDemo {
    public static void main(String args[]) {
        BeingWatched observed = new BeingWatched();
        Watcher observing = new Watcher();
        observed.addObserver(observing);
        observed.counter(10);
    }
}
  • Compile the file using javac ObserverDemo.java
  • Run the file using java ObserverDemo

Output

Summary

The Observable class is basically used to create subclasses that other parts of the program can observe. When an object of a sub class undergoes a change, observing classes are notified.


Similar Articles