The document discusses implementing the Observer design pattern to update multiple display elements whenever new weather data becomes available. It describes creating an interface for display elements to implement, and having the weather data object notify registered display elements by calling an update method. This allows dynamic addition or removal of display elements without coupling them directly to the weather data. The Observer pattern establishes a subscription mechanism so that when one object changes state, its dependents are automatically notified and updated.
Download as PPT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
13 views
OODP Observer
The document discusses implementing the Observer design pattern to update multiple display elements whenever new weather data becomes available. It describes creating an interface for display elements to implement, and having the weather data object notify registered display elements by calling an update method. This allows dynamic addition or removal of display elements without coupling them directly to the weather data. The Observer pattern establishes a subscription mechanism so that when one object changes state, its dependents are automatically notified and updated.
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21
OO Design - Observer Pattern
Object-Oriented Programming The Weather Monitoring application
Đại học Công nghệ - ĐHQG HN OO Design Pattern 2
WeatherData API
Our job: to implement measurementsChanged() so that it updates displ
Đại học Công nghệ - ĐHQG HN OO Design Pattern 3
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 4
First draft solution
Đại học Công nghệ - ĐHQG HN OO Design Pattern 5
A. We are coding to concrete What’s wrong? implementation, not interfaces B. For every new display element, we need to alter code. C. We have no way to add (or remove) display elements at run time D. The display elements don’t implement a common interface. E. We haven’t encapsulated the part that changes F. We are violating encapsulation of the WeatherData class.
Đại học Công nghệ - ĐHQG HN OO Design Pattern 6
A. We are coding to concrete What’s wrong? implementation, not interfaces B. For every new display element, we need to alter code. C. We have no way to add (or remove) display elements at run time D. The display elements don’t implement a common interface. E. We haven’t encapsulated the part that changes F. We are violating encapsulation of the WeatherData class.
Đại học Công nghệ - ĐHQG HN OO Design Pattern 7
The Observer Pattern How newspaper or magazine subscriptions work?
Subjects + Observers = ObserverPattern
Đại học Công nghệ - ĐHQG HN OO Design Pattern 8
The Observer Pattern
Đại học Công nghệ - ĐHQG HN OO Design Pattern 9
The Observer Pattern
Đại học Công nghệ - ĐHQG HN OO Design Pattern 10
The Observer Pattern
Đại học Công nghệ - ĐHQG HN OO Design Pattern 11
Đại học Công nghệ - ĐHQG HN OO Design Pattern 12 Đại học Công nghệ - ĐHQG HN OO Design Pattern 13 Đại học Công nghệ - ĐHQG HN OO Design Pattern 14 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 15
In Class Diagram
Đại học Công nghệ - ĐHQG HN OO Design Pattern 16
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 17
Đại học Công nghệ - ĐHQG HN OO Design Pattern 18 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 19
Đại học Công nghệ - ĐHQG HN OO Design Pattern 20 Homework Reimplement using java.util.Observable