Khangharoth

April 7, 2006

Tutorial On Observer Pattern

Filed under: Java,Software Design — khangharoth @ 12:18 am

Well to start off …Observer pattern is used when An object needs to notify other object(s) if it changes. .
So observer pattern has

1) Subject or Source : on which any action will happen.

2) One or more Observers : Whom Source needs to notify if any change has taken place .

Algorith Used In Observer Pattern :

1) Source have some properties say private String name, private float price;
2) Source has a collection List say ArralList or Vector .This contains refrences of all the Observers who had registerd with source to get Notified.
3) Source has a addObserver(Observer o) method so all objects which implements Observer interface can get registerd to listen the Notification.
4)Now if any changes will be done on price ,name Source will call notifyObservers() and from the collection list Source knows which all objects needs to be notified.

A sample code will be like

package observer;

import java.util.Observable;

/**
* A subject to observe!
*
*/

public class ConcreteSubject extends Observable {
 private String name;

 private float price;

 public ConcreteSubject(String name, float price) {
  this.name = name;
  this.price = price;
  System.out.println("ConcreteSubject created: " + name + " at + price");
 }

 public String getName() {
  return name;
 }

 public float getPrice() {
  return price;
 }

 public void setName(String name) {
  this.name = name;
  setChanged();
  notifyObservers(name);
 }

 public void setPrice(float price) {
  this.price = price;
  setChanged();
  notifyObservers(new Float(price));
 }
}

———————————————————–

package observer;

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

public class NameObserver implements Observer {
 private String name;

 public NameObserver() {
  name = null;
  System.out.println("NameObserver created: Name is " + name);
 }

 public void update(Observable obj, Object arg) {
  if (arg instanceof String) {
   name = (String) arg;
   System.out.println("NameObserver: Name changed to " + name);
  } else {
   System.out.println("NameObserver: Some other change to subject!");
  }
 }
}

————————————————————————

package observer;

import java.util.Observable;
import java.util.Observer;
/**
*
*
*
*/
public class PriceObserver implements Observer {
 private float price;

 public PriceObserver() {
  price = 0;
  System.out.println("PriceObserver created: Price is " + price);
 }

 public void update(Observable obj, Object arg) {
  if (arg instanceof Float) {
   price = ((Float) arg).floatValue();
   System.out.println("PriceObserver: Price changed to " + price);
  } else {
   System.out.println("PriceObserver: Some other change to subject!");
  }
 }
}

————————————————————————————-

package observer;
/**
*
*/
public class TestObservers {
 public static void main(String args[]) {

  // Create the Subject and Observers.
  ConcreteSubject s = new ConcreteSubject("Corn Pops", 1.29f);
  NameObserver nameObs = new NameObserver();
  PriceObserver priceObs = new PriceObserver();

  // Add those Observers!
  s.addObserver(nameObs);
  s.addObserver(priceObs);

  // Make changes to the Subject.
  s.setName("Frosted Flakes");
  s.setPrice(4.57f);
  s.setPrice(9.22f);
  s.setName("Sugar Crispies");
 }
}

Hope this will give you some insight of Observer pattern……if you need any more help……just mail me

Advertisements

Create a free website or blog at WordPress.com.