27
Observer Behavioral Pattern

Observer Behavioral Pattern. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified

Embed Size (px)

Citation preview

Page 1: Observer Behavioral Pattern. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified

Observer

Behavioral Pattern

Page 2: Observer Behavioral Pattern. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified

Intent

• Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

• A.k.a Dependents, Publish-Subscribe

Page 3: Observer Behavioral Pattern. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified

Motivation• A common side-effect of partitioning a system into a collection of

cooperating classes is the need to maintain consistency between related objects. You don‘t want to achieve consistency by making the classes tightly coupled, because that reduces their reusability

• For example, many graphical user interface toolkits separate the presentational aspects of the user interface from the underlying application data. Classes defining application data and presentations can be reused independently. They can work together, too.

• Both a spreadsheet object and bar chart object can depict information in the same application data object using different presentations. The spreadsheet and the bar chart don't know about each other, thereby letting you reuse only the one you need. But they behave as though they do. When the user changes the information in the spreadsheet, the bar chart reflects the changes immediately, and vice versa

Page 4: Observer Behavioral Pattern. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified

Motivation

• This behavior implies that the spreadsheet and bar chart are dependent on the data object and therefore should be notified of any change in its state. And there’s no reason to limit the number of dependent objects to two; there may be any number of different user interfaces to the same data

• The Observer pattern describes how to establish these relationships. The key objects in this pattern are subject and observer. 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

Page 5: Observer Behavioral Pattern. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified

Motivation

• This kind of interaction is also known as publish-subscribe. The subject is the publisher of notifications. It sends out these notifications without having to know who its observers are. Any number of observers can subscribe to receive notifications

Page 6: Observer Behavioral Pattern. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified

Applicability

• Use the Observer pattern in any of the following situations:– When an abstraction has two aspects, one dependent on

the other. Encapsulating these aspects in separate objects lets you vary and reuse them independently.

– When a change to one object requires changing others, and you don't know how many objects need to be changed.

– When an object should be able to notify other objects without making assumptions about who these objects are. In other words, you don't want these objects tightly coupled.

Page 7: Observer Behavioral Pattern. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified

Structure

Page 8: Observer Behavioral Pattern. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified

Participants• Subject

– knows its observers. Any number of Observer objects may observe a subject.

– provides an interface for attaching and detaching Observer objects.• Observer

– defines an updating interface for objects that should be notified of changes in a subject.

• ConcreteSubject– stores state of interest to Concrete Observer objects.– sends a notification to its observers when its state changes.

• ConcreteObserver– maintains a reference to a Concrete Subject object.– stores state that should stay consistent with the subject's.– implements the Observer updating interface to keep its state consistent

with the subject's.

Page 9: Observer Behavioral Pattern. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified

Collaborations

• ConcreteSubject notifies its observers whenever a change occurs that could make its observers' state inconsistent with its own.

• After being informed of a change in the concrete subject, a ConcreteObserver object may query the subject for information. ConcreteObserver uses this information to reconcile its state with that of the subject.

Page 10: Observer Behavioral Pattern. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified

Collaborations

• Note how the Observer object that initiates the change request postpones its update until it gets a notification from the subject. Notify is not always called by the subject. It can be called by an observer or by another kind of object entirely.

Page 11: Observer Behavioral Pattern. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified

Consequences

• The Observer pattern lets you vary subjects and observers independently. You can reuse subjects without reusing their observers, and vice versa. It lets you add observers without modifying the subject or other observers

• Abstract coupling between Subject and Observer – All a subject knows is that it has a list of observers, each conforming to the simple interface of the abstract Observer class. The subject doesn't know the concrete class of any observer. Thus the coupling between subjects and observers is abstract and minimal

Page 12: Observer Behavioral Pattern. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified

Consequences

• Support for broadcast communication – Unlike an ordinary request, the notification that a subject sends needn't specify its receiver. The notification is broadcast automatically to all interested objects that subscribed to it. The subject doesn't care how many interested objects exist; its only responsibility is to notify its observers. This gives you the freedom to add and remove observers at any time. It's up to the observer to handle or ignore a notification

Page 13: Observer Behavioral Pattern. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified

Weather Monitoring Application

Weather Station

HumiditySensor

TempSensor

PressureSensor

Weather DataObject

DisplayDevice

PullsData

displays

Page 14: Observer Behavioral Pattern. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified

What needs to be done?

/** Call this method* whenever measurements are* Updated*/Public void measurementsChanged(){

// your code goes here}

WeatherData

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

Update threedifferent displays

Page 15: Observer Behavioral Pattern. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified

Problem specification

• weatherData class has three getter methods• measurementsChanged() method called

whenever there is a change• Three display methods needs to be supported:

current conditions, weather statistics and simple forecast

• System should be expandable

Page 16: Observer Behavioral Pattern. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified

First cut at implementationpublic class WeatherData {

public void measurementsChanged(){float temp = getTemperature();float humidity = getHumidity();float pressure = getPressure();

currentConditionsDisplay.update (temp, humidity, pressure);statisticsDisplay.update (temp, humidity, pressure);forecastDisplay.update (temp, humidity, pressure);

}// other methods

}

Page 17: Observer Behavioral Pattern. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified

First cut at implementationpublic class WeatherData {

public void measurementsChanged(){float temp = getTemperature();float humidity = getHumidity();float pressure = getPressure();

currentConditionsDisplay.update (temp, humidity, pressure);statisticsDisplay.update (temp, humidity, pressure);forecastDisplay.update (temp, humidity, pressure);

}// other methods

}By coding to concrete implementationsthere is no way to add additional displayelements without making code change

Area of change which can beManaged better by encapsulation

Page 18: Observer Behavioral Pattern. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified

Basis for observer pattern

• Fashioned after the publish/subscribe model• Works off similar to any subscription model– Buying newspaper– Magazines– List servers

Page 19: Observer Behavioral Pattern. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified

Observer Pattern – Weather data

<<interface>>Subject

registerObserver()removeObserver()notifyObservers()

<<interface>>Observer

update()

observers

WeatherData

registerObserver()removeObserver()notifyObservers()getTemperature()getPressure()measurementsChanged()

subject

<<interface>>DisplayElement

display()

StatisticsDisplay

update()display()

ForecastDisplay

update()display()

CurrentConditionsDisplay

update()display()

Page 20: Observer Behavioral Pattern. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified

Weather data interfacespublic interface subject {

void registerObserver(Observer o);//Takes observer as an argument void removeObserver(Observer o);//Takes observer as an argument void notifyObserver(); //Is called to notify all observers when the subject’s

} // state is changed public interface Observer {//The update() gets state values as argument from Subject when weather

//measurement changes void update(float temp,float humidity,float pressure); } public interface DisplayElement { void display(); }

Page 21: Observer Behavioral Pattern. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified

Implementing subject interface

public class WeatherData :subject { private ArrayList observers; //Array list to hold the observers

private float temperature; private float humidity; private float pressure;

public WeatherData() { observers=new ArrayList(); // creating the array list in constructor }

Page 22: Observer Behavioral Pattern. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified

Register and unregisterpublic void registerObserver(Observer o) {

observers.Add(o);// when we have a new observer we add it // to the list of observers}

public void removeObserver(Observer o) {int i = observers.indexOf(o);if (i >= 0) {

observers.Remove(i);}

}

Page 23: Observer Behavioral Pattern. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified

Notify methods public void notifyObserver() {// We Notify all observers about the state by calling update() method on all foreach (Observer obs in observers) { obs.update(temperature, humidity, pressure); } } void measurementChanged() {//We notify the observers when we get updated weather measurements notifyObserver(); } public void setMeasurement(float temperature,float humidity,float pressure) {//Setting dummy values of weather station this.temperature = temperature; this.humidity = humidity; this.pressure =

pressure; measurementChanged(); }

Page 24: Observer Behavioral Pattern. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified

public class currentConditionDisplay: Observer,DisplayElement { private float temperature; private float humidity; private subject weatherData;

//Passing the subject and using it to register the display as an observer public currentConditionDisplay(subject weatherData) { this.weatherData = weatherData; weatherData.registerObserver(this);

} public void update(float temperature, float humidity, float pressure) {//when update() is called we save the temp and humidity and call display() this.temperature = temperature; this.humidity = humidity; display(); }

Implementing Display Application

Page 25: Observer Behavioral Pattern. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified

Display Methods

public void display() { Console.WriteLine("Current condition:"+temperature+"F

degress and"+humidity+"% humidity"); }

Page 26: Observer Behavioral Pattern. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified

Client Classclass Program { static void Main(string[] args) {

//Declaring the weather data (subject) object WeatherData weatherdata = new WeatherData();

//Creating the display and passing it the subject currentConditionDisplay currentdisplay = new currentConditionDisplay(weatherdata); //Simulating the three new weather measurements

weatherdata.setMeasurement(80,65,30.4f); weatherdata.setMeasurement(82,70,29.2f); weatherdata.setMeasurement(78, 90, 29.2f); Console.ReadKey(); } }

Page 27: Observer Behavioral Pattern. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified

Output Screen