Presentation is loading. Please wait.

Presentation is loading. Please wait.

OBSERVER PATTERN OBSERVER PATTERN Presented By Presented By Ajeet Tripathi ISE2007015.

Similar presentations


Presentation on theme: "OBSERVER PATTERN OBSERVER PATTERN Presented By Presented By Ajeet Tripathi ISE2007015."— Presentation transcript:

1 OBSERVER PATTERN OBSERVER PATTERN Presented By Presented By Ajeet Tripathi ISE2007015

2 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

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

4 Also Known As Publisher-subscriber. Publisher-subscriber. Dependents. Dependents.

5 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.

6 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.

7 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()

8 Participants Subject. Subject. Observer. Observer. Concrete Subject. Concrete Subject. Concrete Observer. Concrete Observer.

9 Collaborations A Concrete subject A concrete observer Another Concrete Observer setstate() notify() update() getstate() update() getstate()

10 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.

11 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(); }

12 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);}}

13 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);}}

14 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 }

15 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"); }}

16 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.

17 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(); }}

18 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.

19 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.

20 Questions *???????????*

21 THANKS


Download ppt "OBSERVER PATTERN OBSERVER PATTERN Presented By Presented By Ajeet Tripathi ISE2007015."

Similar presentations


Ads by Google