0% found this document useful (0 votes)
24 views12 pages

Observer

The Observer pattern describes how to establish a one-to-many dependency between objects so that one object (the subject) can notify its dependents (observers) when its state changes. The key objects are the subject and observers - a subject may have any number of observers, and all observers are notified whenever the subject changes state. This provides loose coupling between subjects and observers - observers can be added or removed at any time without modifying the subject.

Uploaded by

Shohanur Rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views12 pages

Observer

The Observer pattern describes how to establish a one-to-many dependency between objects so that one object (the subject) can notify its dependents (observers) when its state changes. The key objects are the subject and observers - a subject may have any number of observers, and all observers are notified whenever the subject changes state. This provides loose coupling between subjects and observers - observers can be added or removed at any time without modifying the subject.

Uploaded by

Shohanur Rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Observer Pattern

Observer Pattern

 Sometimes we need to a one to many


dependency between objects so that one
object changes state , all its dependents are
notified and updated.
 Observer pattern describes how to establish
these relationships.
Characteristic:
• The key objects in this pattern are subject and
observers.
Observer Pattern(continues…)

• A subject may have any number of dependent


observers.
• All observers are notified whenever the
subject undergoes a change in state.
• In response ,each observer will query the
subject to synchronize its state with the
subject’s state.
Observer Pattern(continues…)

Example:
 We consider that we have to create weather
data object to update three displays for current
condition, weather stats ,and a forecast.
The system must be expendable – other
developer can create new custom display
elements and users can add or remove as
many display elements as they want.
Observer Pattern(continues…)
WeatherData

getTemperature()
getHumidity()
getPressure()
measurementsChanged()

//other methods

Figure:-WeatherData object
Observer Pattern(continues…)
 Wrong Implementation
public class WeatherData
{

// instance variable declarations

public void measurementsChanged()


{
float temp = getTemperature();
float humidity = getHumidity();
float pressure = getPressure();
currentConditionDisplay.update(temp,humidity,pressure);
statisticsDisplay .update(temp,humidity,pressure);
forecastDisplay.update(temp,humidity,pressure);
}

}
Observer Pattern (continues…)

• Previous implementation is wrong because


subject and observer are strictly coupled.
The Power of Loose Coupling:
 The observer Pattern provides an object design where subjects
and observers are loosely coupled.
 We add new observers at any time.
 We never need to modify the subject to add new types of
observers.
 We can reuse subjects and observers independently of each
other.
 Changes to either the subjects or an observers will not affect
the other.
Observer Pattern Structure

<<Subject>> <<Observer>>

Register(observer 0);
display ( );
Notification( );

Remove(observer 0)

WeatherData
Current Forecast

display ( ); display ( );
Observer Pattern(continues…)
weather(1)
public class WeatherData implements Subject
subject {
public interface Subject { private ArrayList observers;
public void registerObserver(Observer o); private float temperature;
public void removeObserver(Observer o); private float humidity;
public void notifyObservers(); private float pressure;
} public WeatherData() {
observers = new ArrayList();
}
public interface Observer { public void registerObserver(Observer o) {
public void update(float temp, float humidity, float observers.add(o);
pressure);
} }
public void removeObserver(Observer o) {
Int i = observers.indexOf(o);
public interface DisplayElement { if (i >= 0) {
public void display(); observers.remove(i);
} }
}
Observer Pattern(continues…)

The WeatherData (2) Display Element


public class CurrentConditionDisplay implements
public void notifyObservers() { Observer, DisplayElement {
for (int i = 0; i < observers.size();i++) { private float temperature;
Observer observer = (Observer) observers.get(i); private float humidity;
observer.update(temperature, humidity, pressure); private Subject weatherData;
} public CurrentConditionDisplay(Subject weatherData) {
} this.weatherData = weatherData;
public void measurementsChanged() { weartherData.registerObserver(this);
notifyObservers(); }
} public void update(float temperature, float humidity, float
public void setMeasurements(float temperature, float pressure) {
humidity, float this.temperature = temperature;
pressure) { this.humidity = humidity;
this.temperature = temperature; display();
this.humidity = humidity; }
this.pressure = pressure; public void display() {
measurementChanged(); System.out.println(“Current condition: ”+temperature+”F
} degree”+
// other WeatherData methods humidity+”% humidity”);
} }
}
Observer Pattern(continues…)
The Weather Station

public class WeatherStation {


public static void main(String[] args) {
WeatherData weatherData = new WeatherData();
CurrentConditionDisplay currentDisplay = new CurrentConditionDisplay(weatherData);
StatisticDisplay statisticDisplay = new StatisticDisplay(weatherData);
ForecastDisplay forecastDisplay = new ForecastDisplay(weatherData);

// simulate the weather


weatherData.setMeasurement(80,65,30.4f);
weatherData.setMeasurement(82,70,29.2f);
weatherData.setMeasurement(78,90,29.2f);
}
}
THANK YOU

You might also like