Download presentation
Presentation is loading. Please wait.
1
OO Design - Observer Pattern
Object-Oriented Programming
2
The Weather Monitoring application
Đại học Công nghệ - ĐHQG HN OO Design Pattern
3
WeatherData API Our job: to implement measurementsChanged() so that it updates displays Đại học Công nghệ - ĐHQG HN OO Design Pattern
4
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
5
First draft solution Đại học Công nghệ - ĐHQG HN OO Design Pattern
6
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
7
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
8
The Observer Pattern How newspaper or magazine subscriptions work?
Subjects + Observers = ObserverPattern Đạ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
The Observer Pattern Đạ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
Đại học Công nghệ - ĐHQG HN
OO Design Pattern
15
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
16
In Class Diagram Đại học Công nghệ - ĐHQG HN OO Design Pattern
17
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
18
Đại học Công nghệ - ĐHQG HN
OO Design Pattern
19
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
20
Đại học Công nghệ - ĐHQG HN
OO Design Pattern
21
Homework Reimplement using java.util.Observable
Đại học Công nghệ - ĐHQG HN OO Design Pattern
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.