Monday, September 13, 2004

Observer Pattern

::OBSERVER PATTERN:: Behavioural Pattern

You might have faced a situation were we would like to display data in more than one form at the same time and have all of the corresponding displays reflect that changes .

Example: You may have a typical bar-chart representation and a tabular representation of stock prices. Each time the price changes we'd expect the both representation to take up the changes.. right.. that's what Observer Pattern does.

Here we have OBSERVER and a SUBJECT, the OBSERVER registers or agrees to listen or observe that SUBJECT object through a predefined interface, and when any changes happen to the SUBJECT the OBSERVER comes to know about that changes through the already agreed interface.

Rest is upto you guys to explore...


Below example is adapted from
http://www.javaworld.com/javaworld/javaqa/2001-05/04-qa-0525-observer.html.






/*
* Subject.java
*
* Created on September 13, 2004, 10:36 AM
*/

public interface Subject {
public void addObserver(Observer observer);
public void removeObserver(Observer observer);
}
******************************************************************


/*
* Observer.java
*
* Created on September 13, 2004, 10:40 AM
*/

public interface Observer {
public void update(Subject subject);
}
*******************************************************************


/*
* IntegerDataBag.java
*
* Created on September 13, 2004, 10:46 AM
*/

import java.util.*;

public class IntegerDataBag implements Subject{

private ArrayList list;
private ArrayList observers;

/** Creates a new instance of IntegerDataBag */
public IntegerDataBag() {
list = new ArrayList();
observers = new ArrayList();
}

public void add(Integer value){
list.add(value);
notifyObservers();
}

public void remove(Integer value){
list.remove(value);
notifyObservers();
}

public Iterator iterator(){
return list.iterator();
}

public void notifyObservers(){
Iterator iterators = observers.iterator();
System.out.println("in notifyObservers");
while( iterators.hasNext() ){
Observer ob = (Observer)iterators.next();
ob.update(this);
System.out.println("inside loop");
}
}

public void addObserver(Observer observer) {
observers.add( observer );
}

public void removeObserver(Observer observer) {
observers.remove(observer);

}
}
***********************************************************************


/*
* IntegerAdder.java
*
* Created on September 13, 2004, 11:32 AM
*/

import java.util.Iterator;

public class IntegerAdder implements Observer{
private IntegerDataBag integerDataBag;

/** Creates a new instance of IntegerAdder */
public IntegerAdder(IntegerDataBag sub) {
this.integerDataBag = sub;
integerDataBag.addObserver(this);
}

public void update(Subject subject) {
Iterator iterator = integerDataBag.iterator();
int sum = 0;
while( iterator.hasNext() ){
Integer intValue = (Integer)iterator.next();
sum = sum + intValue.intValue();

}
System.out.println("The new sum is : "+ sum );
}

}
***********************************************************************

/*
* Driver.java
*
* Created on September 13, 2004, 11:56 AM
*/

public class Driver {

IntegerDataBag integerBox;

/** Creates a new instance of Driver */
public Driver() {
integerBox = new IntegerDataBag();
}

public void testDrive(){
Integer i1 = new Integer(1);
Integer i2 = new Integer(1);
Integer i3 = new Integer(1);
Integer i4 = new Integer(1);

// Registering or Subscribing IntegerAdder
IntegerAdder adder = new IntegerAdder(integerBox);

integerBox.add(i1);
integerBox.add(i2);
integerBox.add(i3);

integerBox.remove(i3);



}

public static void main(String args[]){
Driver driver = new Driver();
driver.testDrive();

}

}
****************************************************************

1 comment:

Susheel Varma said...

Hi Anumodh,

Good eplanation of the Observer pattern, but aren't you just repeating a standard Behavioral Design Pattern. Wikipedia and the Portland repository has good list of the standard and fundamental patterns which developers can use.

Best Wishes,

Susheel