OBSERVER PATTERN OBSERVER PATTERN Presented By Presented By Ajeet Tripathi ISE
Pattern Classification - Observer pattern is Behavioral design pattern. pattern. (The Observer pattern defines the way a number of classes can be notified of a change) (The Observer pattern defines the way a number of classes can be notified of a change)Observer patternObserver pattern
Intent - Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
Also Known As Publisher-subscriber. Publisher-subscriber. Dependents. Dependents.
Motivation A common side-effect of partitioning a system into a collection of cooperating classes is the need to maintain consistency between related objects. & we don't want to achieve consistency by making the classes tightly coupled, because that reduces their reusability. A common side-effect of partitioning a system into a collection of cooperating classes is the need to maintain consistency between related objects. & we don't want to achieve consistency by making the classes tightly coupled, because that reduces their reusability.
Applicability When an abstraction has two aspects, one dependent on the other. Encapsulating these aspects in separate objects lets us vary and reuse them independently. When an abstraction has two aspects, one dependent on the other. Encapsulating these aspects in separate objects lets us vary and reuse them independently. When a change to one object requires changing others, and us don't know how many objects need to be changed. When a change to one object requires changing others, and us 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, us don't want these objects tightly coupled. When an object should be able to notify other objects without making assumptions about who these objects are. In other words, us don't want these objects tightly coupled.
Structure Concrete subject > Subject register observer() remove observer() notify observer() > observer Update() Concrete Observer Update() // other observer Specific methods// register observer() remove observer() notify observer() getstate() setstate()
Participants Subject. Subject. Observer. Observer. Concrete Subject. Concrete Subject. Concrete Observer. Concrete Observer.
Collaborations A Concrete subject A concrete observer Another Concrete Observer setstate() notify() update() getstate() update() getstate()
Consequences Abstract coupling between Subject and Observer. Abstract coupling between Subject and Observer. Support for broadcast communication. Support for broadcast communication. Unexpected updates. Unexpected updates.
Implementation public interface Subject { public void registerObserver(Observer o); public void removeObserver(Observer o); public void notifyObservers(); } public interface Observer { public void update(float temp, float humidity, float pressure); } public interface DisplayElement { public void display(); }
public class WeatherData implements Subject { private ArrayList observers; private float temperature; private float humidity; private float pressure; public WeatherData() { observers = new ArrayList(); } public void registerObserver(Observer o) { observers.add(o);} public void removeObserver(Observer o) { int i = observers.indexOf(o); if (i >= 0) { observers.remove(i);}}
public class WeatherData implements Subject { private ArrayList observers; private float temperature; private float humidity; private float pressure; public WeatherData() { observers = new ArrayList(); } public void registerObserver(Observer o) { observers.add(o);} public void removeObserver(Observer o) { int i = observers.indexOf(o); if (i >= 0) { observers.remove(i);}}
public void notifyObservers() { for (int i = 0; i < observers.size(); i++) { Observer observer = (Observer)observers.get(i); observer.update(temperature, humidity, pressure); }} public void measurementsChanged() { notifyObservers();} public void setMeasurements(float temperature, float humidity, float pressure) { this.temperature = temperature; this.humidity = humidity; this.pressure = pressure; measurementsChanged();} // other WeatherData methods here - getters }
public class CurrentConditionsDisplay implements Observer, DisplayElement { private float temperature; private float humidity; private Subject weatherData; public CurrentConditionsDisplay(Subject weatherData) { this.weatherData = weatherData; weatherData.registerObserver(this);} public void update(float temperature, float humidity, float pressure) { this.temperature = temperature; this.humidity = humidity; display();} public void display() { System.out.println("Current conditions: " + temperature + "F degrees and " + humidity + "% humidity"); }}
Loose Coupling When two objects are loosely coupled, they can interact, but have very little knowledge of each other When two objects are loosely coupled, they can interact, but have very little knowledge of each other The observer Pattern provides an object design where subjects and observers are loosely coupled. The observer Pattern provides an object design where subjects and observers are loosely coupled.
Push vs Pull Solution “pushes” data to the observers Solution “pushes” data to the observers public void update(float temperature, float humidity, float pressure) { this.temperature = temperature; this.temperature = temperature; this.humidity = humidity; display();} Observers can “pull” the specific data they need from the subject Observers can “pull” the specific data they need from the subject public void update(Subject subject) { if (subject instanceof WeatherData) { if (subject instanceof WeatherData) { WeatherData weatherData = (WeatherData) subject; WeatherData weatherData = (WeatherData) subject; this.temperature = weatherData.getTemperature(); this.temperature = weatherData.getTemperature(); this.humidity = weatherData.getHumidity(); this.humidity = weatherData.getHumidity(); display(); display(); }}
Other Implementation Issues Observing more than one subject Observing more than one subject –Which subject is calling update()? Where is notify() called? Where is notify() called? –In state setting methods on the subject; –In an external client after several state changes Update different observers based on “interest” Update different observers based on “interest” –Why update all observers if some observers are only interested in particular state changes.
Related Patterns Adaptor Adaptor –Can be used to allow objects that do not implement the required interface to participate in the Observer Pattern. Mediator Mediator –Can be implemented as an observer. Colleague classes act as subjects, sending notifications to the mediator whenever they change state.
Questions *???????????*
THANKS