observer pattern

16
The Observer Pattern

Upload: akshat-vig

Post on 13-Dec-2014

2.528 views

Category:

Technology


3 download

DESCRIPTION

Observer Design Pattern- Head First

TRANSCRIPT

Page 1: Observer Pattern

The Observer Pattern

Page 2: Observer Pattern

Weather Monitoring Application

Weather Station

Device that receives the weather data

Weather Data Object

Guy that tracks the data coming from weather

station and updates the display

Display

Shows the current weather conditions.

Page 3: Observer Pattern

Job description

Create an application that uses the weather data Object to update the three displays for weather conditions.

The three displays are current conditions, weather stats and forecast

Page 4: Observer Pattern

Weather Data Class

• getTemprature()

• getPressure()

• getHumidity()

• measurementChanged()

Weather Data Class

Page 5: Observer Pattern

Our Task

Implement measurementchanged()

function so that it updates the displays

accordingly

Page 6: Observer Pattern

Simple Solution

• Float temp = getTemperature();

• Float Pr= getPressure();

• Float Hum = getHumidity();

• currentConditionsDisplay.update(temp,Pr,Hum);

• statisticsDisplay.update(temp,Pr,Hum);

• forecastDisplay.update(temp,Pr,Hum);

measurementChanged() {

Page 7: Observer Pattern

Problems with the previous approach

We are coding to concrete implementation rather then to an interface.

We have not encapsulated the part that changes from the part that remains constant. (Display Changes)

We have no way to add or remove the weather displays at run time.

If we want to add new display we have to alter code.

Page 8: Observer Pattern

The Observer Pattern

Similar to Newspaper subscription.

Publishers publish and subscribers read.

Once subscribers unsubscribe publishers wont get anything to read.

Once subscribers subscribe again they get to read.

Publishers are Subject and subscribers are Observer.

“The Observer pattern defines a one to many relationship between a set of objects. When the state of one changes it is notified to all the others.”

Page 9: Observer Pattern

Design: Class Diagram<<interface Subject>>Removesubscriber()

addSubscriber()notifyObservers()

<<interface Observer>>update()

<<interface Display>>disp()

WeatherData

addSubscriber()removerSubscriber()

notifyObservers()measurementChanged()

getTemprature()getHumidity()getPressure()

CurrentConditionDisp

Update();disp();

StatisticsDisplay

Update();disp();

ForecastDisplay

Update();disp();

Page 10: Observer Pattern

Implementation (Subject)

public class WeatherData implements Subject{private ArrayList Observers;private float temp,press,hum;public WeatherData(){ Observers = new ArrayList();}Publc void removeObserver(Observer o){

int index = Observers.indexOf(o);Observers.remove(i);

}Public void notifyObservers(){

for(I =0;i<Observers.length();i++) {Observer o = (Observer)Observers.get(i);o.update(temp,hum,press);

}}Public void measurementChanged(){

notifyObservers();}

public void addObserver(Observer o){Observers.add(o);}

Page 11: Observer Pattern

Implementation (Observer)

public class CurrentConditionsDisplay implements Observer,Display {private float temp,hum,press;private Subject weatherData;

public CurrentConditionsDisplay (Subject s){this.weatherData = s;

weatherData.addObserver(this);}Public void update(float temp, float press, float hum){

this.temp= temp; this. Press = press;display();

}public void display(){

System.out.println(“ The current conditions of temperature are good : “+temp);}

Page 12: Observer Pattern

Java has built in support for Observer

• You don’t have to implement the addObserver, removeObserver etc since these are already implemented in Observable class (not an interface).

• Both are present in the java.util package.

• You can also implement either pull or push style to your observers.

• For an object to become Observer:– Implement the Observer Interface and call

addObserver() on any Observable Object.

Page 13: Observer Pattern

Cont…

• For the Observable to send notifications:– You must first call the protected setChanged() method

to signify that the state has changed.

– Then call one of the two notifyObservers() methos• notifyObservers()

• notifyObservers(Object arg)– The argument passed is the data Obect.

– For an Observer• Update(Observable o, Object arg)

– Implements the update method. If you want the push model you can put the data in the dataObject else in case of pull method the Observer can get the data when ever it wants.

Page 14: Observer Pattern

Dark Side of Observer Java Built in Pattern

• The Observable is a class not an interface. Any already defined class which has already extended a class can not subclass it.

• You cant create your own implementation that plays well with Java Built in Observer API.

• Observable protects the setChanged() method hence any class has to subclass It if they want to use Observable. This hampers the design principle “favor composite over inheritance”

• Swing uses the Observer Pattern. Since every button has a number of listeners. Listeners are observers which wait for any change in the state of the button and respond accordingly.

Page 15: Observer Pattern

References…

• Head First Design Patterns

Page 16: Observer Pattern