OO Design - Observer Pattern Object-Oriented Programming
The Weather Monitoring application Đại học Công nghệ - ĐHQG HN OO Design Pattern
WeatherData API Our job: to implement measurementsChanged() so that it updates displays Đại học Công nghệ - ĐHQG HN OO Design Pattern
Our job to implement measurementsChanged() so that it updates the displays: Given: Getter methods available for temperature, humidity, and pressure measurementsChanged() is called each time new weather data is available We need to implement three displays that update every time WeatherData has new data Can add new displays and remove displays Đại học Công nghệ - ĐHQG HN OO Design Pattern
First draft solution Đại học Công nghệ - ĐHQG HN OO Design Pattern
What’s wrong? We are coding to concrete implementation, not interfaces For every new display element, we need to alter code. We have no way to add (or remove) display elements at run time The display elements don’t implement a common interface. We haven’t encapsulated the part that changes We are violating encapsulation of the WeatherData class. What’s wrong? Đại học Công nghệ - ĐHQG HN OO Design Pattern
What’s wrong? We are coding to concrete implementation, not interfaces For every new display element, we need to alter code. We have no way to add (or remove) display elements at run time The display elements don’t implement a common interface. We haven’t encapsulated the part that changes We are violating encapsulation of the WeatherData class. What’s wrong? Đại học Công nghệ - ĐHQG HN OO Design Pattern
The Observer Pattern How newspaper or magazine subscriptions work? Subjects + Observers = ObserverPattern Đại học Công nghệ - ĐHQG HN OO Design Pattern
The Observer Pattern Đại học Công nghệ - ĐHQG HN OO Design Pattern
The Observer Pattern Đại học Công nghệ - ĐHQG HN OO Design Pattern
The Observer Pattern Đại học Công nghệ - ĐHQG HN OO Design Pattern
Đại học Công nghệ - ĐHQG HN OO Design Pattern
Đại học Công nghệ - ĐHQG HN OO Design Pattern
Đại học Công nghệ - ĐHQG HN OO Design Pattern
The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically Đại học Công nghệ - ĐHQG HN OO Design Pattern
In Class Diagram Đại học Công nghệ - ĐHQG HN OO Design Pattern
Benefits of the Observer Pattern Subjects and observers can interact but have very little knowledge of each other. The only thing the subject knows about an observer is that it implements the Observer interface We can add new observers at any time. We never need to modify the subject to add new type of observers We can reuse subjects or observers independently of each other Changes to either the subject or an observer will not affect the other Đại học Công nghệ - ĐHQG HN OO Design Pattern
Đại học Công nghệ - ĐHQG HN OO Design Pattern
Back to your task to implement measurementsChanged() so that it updates the displays: Given: Getter methods available for temperature, humidity, and pressure measurementsChanged() is called each time new weather data is available We need to implement three displays that update every time WeatherData has new data Can add new displays and remove displays Đại học Công nghệ - ĐHQG HN OO Design Pattern
Đại học Công nghệ - ĐHQG HN OO Design Pattern
Homework Reimplement using java.util.Observable Đại học Công nghệ - ĐHQG HN OO Design Pattern