observer pattern observer pattern presented by presented by ajeet tripathi ise2007015

21
OBSERVER PATTERN OBSERVER PATTERN Presented By Presented By Ajeet Ajeet Tripathi Tripathi ISE2007015 ISE2007015

Upload: cornelius-burns

Post on 20-Jan-2018

221 views

Category:

Documents


1 download

DESCRIPTION

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

TRANSCRIPT

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

OBSERVER PATTERNOBSERVER PATTERN Presented Presented

ByByAjeet Ajeet

TripathiTripathiISE2007015ISE2007015

Page 2: OBSERVER PATTERN OBSERVER PATTERN Presented By Presented By Ajeet Tripathi ISE2007015

Pattern ClassificationPattern Classification--Observer pattern is Behavioral Observer pattern is Behavioral

designdesign pattern.pattern. (The (The ObserverObserver patternpattern defines defines

the way a number of classes can be the way a number of classes can be notified of a change)notified of a change)

Page 3: OBSERVER PATTERN OBSERVER PATTERN Presented By Presented By Ajeet Tripathi ISE2007015

IntentIntent--Define a one-to-many dependency Define a one-to-many dependency

between objects so that when one between objects so that when one object changes state, all its object changes state, all its dependents are notified and updated dependents are notified and updated automatically. automatically.

Page 4: OBSERVER PATTERN OBSERVER PATTERN Presented By Presented By Ajeet Tripathi ISE2007015

Also Known AsAlso Known As• Publisher-subscriber.Publisher-subscriber.• Dependents.Dependents.

Page 5: OBSERVER PATTERN OBSERVER PATTERN Presented By Presented By Ajeet Tripathi ISE2007015

MotivationMotivation• A common side-effect of partitioning A common side-effect of partitioning

a system into a collection of a system into a collection of cooperating classes is the need to cooperating classes is the need to maintain consistency between maintain consistency between related objects. & we don't want to related objects. & we don't want to achieve consistency by making the achieve consistency by making the classes tightly coupled, because that classes tightly coupled, because that reduces their reusability. reduces their reusability.

Page 6: OBSERVER PATTERN OBSERVER PATTERN Presented By Presented By Ajeet Tripathi ISE2007015

ApplicabilityApplicability• When an abstraction has two aspects, one When an abstraction has two aspects, one

dependent on the other. Encapsulating these dependent on the other. Encapsulating these aspects in separate objects lets us vary and reuse aspects in separate objects lets us vary and reuse them independently. them independently.

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

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

Page 7: OBSERVER PATTERN OBSERVER PATTERN Presented By Presented By Ajeet Tripathi ISE2007015

StructureStructure

Concrete subject

<<Interface>> Subject

register observer()remove observer()notify observer()

<<interface>> observer

Update()

Concrete Observer

Update()//other observerSpecific methods//

register observer()remove observer()notify observer()getstate()setstate()

Page 8: OBSERVER PATTERN OBSERVER PATTERN Presented By Presented By Ajeet Tripathi ISE2007015

ParticipantsParticipants• Subject.Subject.• Observer.Observer.• Concrete Subject.Concrete Subject.• Concrete Observer.Concrete Observer.

Page 9: OBSERVER PATTERN OBSERVER PATTERN Presented By Presented By Ajeet Tripathi ISE2007015

CollaborationsCollaborationsA Concrete subject A concrete observer Another Concrete

Observer

setstate()

notify()

update()getstate()

update()

getstate()

Page 10: OBSERVER PATTERN OBSERVER PATTERN Presented By Presented By Ajeet Tripathi ISE2007015

Consequences Consequences • Abstract coupling between Abstract coupling between

Subject and Observer .Subject and Observer .• Support for broadcast Support for broadcast

communication. communication. • Unexpected updates. Unexpected updates.

Page 11: OBSERVER PATTERN OBSERVER PATTERN Presented By Presented By Ajeet Tripathi ISE2007015

ImplementationImplementationpublic interface Subject {public interface Subject {

public void registerObserver(Observer o);public void registerObserver(Observer o);public void removeObserver(Observer o);public void removeObserver(Observer o);public void notifyObservers();public void notifyObservers();

}}

public interface Observer {public interface Observer {public void update(float temp, float humidity, float pressure);public void update(float temp, float humidity, float pressure);

}}

public interface DisplayElement {public interface DisplayElement {public void display();public void display();

}}

Page 12: OBSERVER PATTERN OBSERVER PATTERN Presented By Presented By Ajeet Tripathi ISE2007015

public class WeatherData implements Subject {public class WeatherData implements Subject {private ArrayList observers;private ArrayList observers;private float temperature;private float temperature;private float humidity;private float humidity;private float pressure;private float pressure;

public WeatherData() {public WeatherData() {observers = new ArrayList();observers = new ArrayList();}}

public void registerObserver(Observer o) {public void registerObserver(Observer o) {observers.add(o);observers.add(o);}}

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

Page 13: OBSERVER PATTERN OBSERVER PATTERN Presented By Presented By Ajeet Tripathi ISE2007015

public class WeatherData implements Subject {public class WeatherData implements Subject {private ArrayList observers;private ArrayList observers;private float temperature;private float temperature;private float humidity;private float humidity;private float pressure;private float pressure;

public WeatherData() {public WeatherData() {observers = new ArrayList();observers = new ArrayList();

}}

public void registerObserver(Observer o) {public void registerObserver(Observer o) {observers.add(o);observers.add(o);

}}

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

observers.remove(i);observers.remove(i);}}

}}

Page 14: OBSERVER PATTERN OBSERVER PATTERN Presented By Presented By Ajeet Tripathi ISE2007015

public void notifyObservers() {public void notifyObservers() {

for (int i = 0; i < observers.size(); i++) {for (int i = 0; i < observers.size(); i++) {Observer observer = (Observer)observers.get(i);Observer observer = (Observer)observers.get(i);observer.update(temperature, humidity, pressure);observer.update(temperature, humidity, pressure);

}}}}

public void measurementsChanged() {public void measurementsChanged() {notifyObservers();notifyObservers();

}}

public void setMeasurements(float temperature, float humidity, float pressure) {public void setMeasurements(float temperature, float humidity, float pressure) {this.temperature = temperature;this.temperature = temperature;this.humidity = humidity;this.humidity = humidity;this.pressure = pressure;this.pressure = pressure;measurementsChanged();measurementsChanged();

}}

// other WeatherData methods here - getters// other WeatherData methods here - getters}}

Page 15: OBSERVER PATTERN OBSERVER PATTERN Presented By Presented By Ajeet Tripathi ISE2007015

public class CurrentConditionsDisplay implements Observer, DisplayElement {public class CurrentConditionsDisplay implements Observer, DisplayElement {

private float temperature;private float temperature;private float humidity;private float humidity;private Subject weatherData;private Subject weatherData;

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

}}

public void update(float temperature, float humidity, float pressure) {public void update(float temperature, float humidity, float pressure) {this.temperature = temperature;this.temperature = temperature;this.humidity = humidity;this.humidity = humidity;display();display();

}}

public void display() {public void display() {System.out.println("Current conditions: " + temperature System.out.println("Current conditions: " + temperature

+ "F degrees and " + humidity + "% humidity");+ "F degrees and " + humidity + "% humidity");}}

}}

Page 16: OBSERVER PATTERN OBSERVER PATTERN Presented By Presented By Ajeet Tripathi ISE2007015

Loose CouplingLoose Coupling• When two objects are loosely When two objects are loosely

coupled, they can interact, but have coupled, they can interact, but have very little knowledge of each othervery little knowledge of each other

• The observer Pattern provides an The observer Pattern provides an object design where subjects and object design where subjects and observers are loosely coupled .observers are loosely coupled .

Page 17: OBSERVER PATTERN OBSERVER PATTERN Presented By Presented By Ajeet Tripathi ISE2007015

Push vs PullPush vs Pull• Solution “pushes” data to the observersSolution “pushes” data to the observers

public void update(float temperature, float humidity, float pressure) {public void update(float temperature, float humidity, float pressure) { this.temperature = temperature;this.temperature = temperature;

this.humidity = humidity;this.humidity = humidity;display();display();

}}• Observers can “pull” the specific data they need from the Observers can “pull” the specific data they need from the

subjectsubjectpublic void update(Subject 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(); }}}}

Page 18: OBSERVER PATTERN OBSERVER PATTERN Presented By Presented By Ajeet Tripathi ISE2007015

Other Implementation Other Implementation IssuesIssues• Observing more than one subjectObserving more than one subject

– Which subject is calling update()?Which subject is calling update()?• Where is notify() called?Where is notify() called?

– In state setting methods on the subject;In state setting methods on the subject;– In an external client after several state changesIn an external client after several state changes

• Update different observers based on Update different observers based on “interest”“interest”– Why update all observers if some observers are Why update all observers if some observers are

only interested in particular state changes.only interested in particular state changes.

Page 19: OBSERVER PATTERN OBSERVER PATTERN Presented By Presented By Ajeet Tripathi ISE2007015

Related PatternsRelated Patterns• AdaptorAdaptor

– Can be used to allow objects that do not Can be used to allow objects that do not implement the required interface to implement the required interface to participate in the Observer Pattern.participate in the Observer Pattern.

• MediatorMediator– Can be implemented as an observer. Can be implemented as an observer.

Colleague classes act as subjects, Colleague classes act as subjects, sending notifications to the mediator sending notifications to the mediator whenever they change state.whenever they change state.

Page 20: OBSERVER PATTERN OBSERVER PATTERN Presented By Presented By Ajeet Tripathi ISE2007015

QuestionsQuestions

*???????????*???????????**

Page 21: OBSERVER PATTERN OBSERVER PATTERN Presented By Presented By Ajeet Tripathi ISE2007015

THANKSTHANKS