This document discusses the observer design pattern. It defines the observer pattern as defining a one-to-many dependency between objects so that when one object changes state, all its dependents are notified. It provides class and sequence diagrams to illustrate the pattern. It also discusses implementing the observer pattern in Java using the built-in Observable and Observer classes.
3. Observer design pattern is behavioral pattern
Used to assure consistency between objects.
Separation of the objects that is dependent
on each other.
It used to make relation between objects at
run time not compile time.
Used in MVC(mainly Model & GUI part)
4. Define a one-to-many dependency between
objects so that when one object changes
state, all its dependents are notified and
updated automatically.
Object that changes called “Subject”.
Object that receives updates called “Object”.
5.
6.
7. Sequence diagram
observer Subject
Register/att
ach
Change state
trigger
notify()/upda
notification
te
getstate()
8. Minimal coupling between the Subject and
the Observer:
Can reuse subjects without reusing their observers
and vice versa.
Observers can be added without modifying the
subject.
All subject knows is its list of observers so make
decoupling.
Subject does not need to know the concrete class of
an observer.
9. How does the subject keep track of its observers?
Array, linked list, Vector
What if an observer wants to observe more than
one subject?
subject tell the observer who it is via the update
interface.
Who triggers the update?
The subject whenever its state changes.
Can an observer also be a subject?
Yes! Because class can implement more than
one interface.
10. How does Subject send the changed data to
Object?
Two ways:
1-Pull model: Observer invoke method
requesting data
SubjectName.getdata();
2-Push model: Subject passes data to
observer as argument at update ()
Object[i].update(SubjectName,data);
11. We could implement the Observer pattern
from scratch in Java.
But Java provides the Observable/Observer
classes as built-in support for the Observer
pattern.
The java.util.Observable class is the base
Subject class. Any class that wants to be
observed extends this class.
Provides methods to add/delete observers
Provides methods to notify all observers
Uses a Vector for storing the observer references
12. The java.util.Observer interface is the
Observer interface. It must be implemented
by any observer class.
Java.util.Observable class
public synchronized void addObserver(Observer o)
public synchronized void deleteObserver(Observer
o)
protected synchronized void setChanged()
public void notifyObservers()
public void notifyObservers(Object arg)
13. Java.util.Observer interface
• public abstract void update(Observable o, Object
arg)
Let’s see sample code:
14. import java.util.Observable;
import java.util.Observer;
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 setPrice(float price) {
this.price = price;
setChanged();
notifyObservers(new Float(price));
}
}
15. import java .util.Observer;
import java .util.Observable;
public class ConcreteObserver implements Observer {
private float price;
public void NameObserver() {
price =0;
System.out.println("price observer is created is"+price);
}
public void update(Observable obj, Object a) {
price = ((Float)a).floatValue();
System.out.println("PriceObserver: Price changed to " +
price);
}
}
16. public class TestObserver {
/**
* @param args
*/
public static void main(String[] args) {
ConcreteSubject s = new ConcreteSubject("GUI
team",1.29f);
ConcreteObserver o = new ConcreteObserver();
s.addObserver(o);
s.setPrice(4.56f);
s.setPrice(2.3f);
}
}
17. Program output
ConcreteSubject created: GUI team at 1.29
PriceObserver: Price changed to 4.56
PriceObserver: Price changed to 2.3