Doubt in Observer and Observable Pattern

Hi Friends,I am new to java patterns. can any one please provide me the simple examples for Observer and Observable pattern.Thanks in Advance,Rajesh Kannan. N
[186 byte] By [Rajesh_1981a] at [2007-10-2 17:01:37]
# 1

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!

* @author ksingh

*/

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;

/**

*

* @author ksingh2

*

*/

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;

/**

* @author ksingh2

*/

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

khangharotha at 2007-7-13 18:15:25 > top of Java-index,Other Topics,Patterns & OO Design...
# 2
HI khangharoth ,Thank you Vey much. I understood. But i have one query. If we change the Bame Observer then why it's called Number Observer.I didn't Understand this line.can you give me solution for this.Regards,Rajesh Kannan. N
Rajesh_1981a at 2007-7-13 18:15:25 > top of Java-index,Other Topics,Patterns & OO Design...
# 3
sorry coudn't get your Question , please be more specific
khangharotha at 2007-7-13 18:15:25 > top of Java-index,Other Topics,Patterns & OO Design...
# 4
Hi khangharoth,Actually if you change the Name means it only go to NameObserver. but in this program it will go to NumberObserver also.Regards,Rajesh Kannan. N
Rajesh_1981a at 2007-7-13 18:15:25 > top of Java-index,Other Topics,Patterns & OO Design...
# 5

well this is an implementation logic ...in the current code the Source notifies to all Observers who have registered regardless of whether the Observer need that information or not ....so we have logic in Observers for filtering the imformation..

So in NameObserver Class

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!");

}

}

here if arg is of type String than only nameObserver will react

khangharotha at 2007-7-13 18:15:25 > top of Java-index,Other Topics,Patterns & OO Design...
# 6
wher do we use observer pattern in java? what is the benifit od this?
kasima at 2007-7-13 18:15:25 > top of Java-index,Other Topics,Patterns & OO Design...
# 7
Ever hear of google?
jverda at 2007-7-13 18:15:25 > top of Java-index,Other Topics,Patterns & OO Design...
# 8
> wher do we use observer pattern in java? what is the> benifit od this?The pattern has very little to do with Java. If you knew a bit about patterns, you'd know both that, and the benefit. But asking this specific question is pointless
georgemca at 2007-7-13 18:15:25 > top of Java-index,Other Topics,Patterns & OO Design...
# 9
> Ever hear of google?working in google?
kasima at 2007-7-13 18:15:25 > top of Java-index,Other Topics,Patterns & OO Design...
# 10
> > Ever hear of google?> > working in google?Not at the moment. Funny you should mention it though.
jverda at 2007-7-13 18:15:25 > top of Java-index,Other Topics,Patterns & OO Design...