Way2Java

Observer and Observable Java

Observer Observable Introduction

Sometimes, it may be necessary to get notified when the data changes in a data structure. To accomplish this job, the Java designers introduced Observer and Observable classes with JDK 1.0, the starting version itself. Observer is an interface and Observable is a class, both from java.util package. As the names are indicative, Observer observes the Observable. Observer keeps on watching the Observable and when observable gets changed, informs the concerned (who have registered earlier). Similar affect is achieved with listener interfaces and adapter classes, through events, in AWT components and servlets.

Some applications of these two classes are to keep watch on purchasers of some items like explosives or contraband items etc. in a shopping mall. Another one is, if a share value gets changed, it must be informed to the share holder and also the banker where the share is pledged.

With this understanding, now let us go into the programming.

Any class that would like to watch must implement Observer interface. The Observer gets notified whenever Observable gets modified. One Observable object can be watched by many Observer objects.

Any class that would like to be observed (or watched) should extend Observable class. The notifyObservers() method of Observable class informs to all the Observer objects by calling update() method. The update() is defined in Observer interface.

In the following program, two observers exist – Principal and HOD (Head of the Department). The observable is Student. Here, Principal and HOD watch the Student. Whenever the student gets marks less than 40, the principal and HOD are informed.

Example on the usage of classes Observer Observable
import java.util.Observer;
import java.util.Observable;

class Principal implements Observer		// observer one
{
  public void update(Observable oble, Object obj1)	
  {
    System.out.println("Principal is observing " + obj1);
  }
}

class HOD implements Observer		         // observer second
{
  public void update(Observable oble, Object obj1)	
  {
    System.out.println("HOD is observing " + obj1);
  }
}

class Student extends Observable
{
  public void warn(Integer marks)
  {
    setChanged();
    if(marks.intValue() < 40)
    {
      notifyObservers(marks);       
    }
  }
}
public class College
{
  public static void main(String args[])
  {
    Principal p1 = new Principal();
    HOD h1 = new HOD();
    Student s1 = new Student();
    s1.addObserver(p1);
    s1.addObserver(h1);
    System.out.println("Count of Observers on Student: " + s1.countObservers());

    s1.warn(new Integer(34));
    s1.warn(new Integer(25));

    System.out.println("\nAfter disconnecting Principal object.");
    s1.deleteObserver(p1);
    s1.warn(new Integer(20));
  }
}


Output screenshot on Observer Observable

The classes Principal and HOD implements Observer interface as they are interested to observe. The class Student extends Observable as student should get observed.

public void update(Observable oble, Object obj1)
{
System.out.println("HOD is observing " + obj1);
}

update() is the only abstract method of Observer interface which must be overridden. This method is implicitly called when the Observable object gets changed. The Observable object, oble, is passed as first parameter. The code calls notifyObservers() method of Observable class to notify all the observers earlier registered. The second parameter, obj1, is passed by notifyObservers() method.

public void warn(Integer marks)
{
setChanged();
if(marks.intValue() < 40) { notifyObservers(marks); } }

warn() is an user-defined method which calls notifyObservers() method when the marks are less than 40. setChanged() method notifies the Observable object when a change happens. notifyObservers() method requires an object as parameter and for this reason Integer object is passed as parameter (and not int).

s1.addObserver(p1);
s1.addObserver(h1);

addObserver() is the method of Observable class with which an Observable object links with Observer interface. Here, the Student object, s1, is linked (registered) with two observers, p1 of Principal and h1 of HOD. This is the place where actual link between Observer and Observable comes in. The countObservers() method returns the number of Observer objects registered with the Observable object.

s1.deleteObserver(p1);

The Observable object can unregister or delink with the Observer with deleteObserver() method. As the Principal object is delinked, when the Student object, s1 gets 20 marks, he is not notified but HOD is notified. See the screenshot.