Observer Pattern: Luca Viganò
Observer Pattern: Luca Viganò
Luca Vigan
Dipartimento di Informatica Universit di Verona
Observer
1 / 26
Dont miss out when something interesting happens! There is a pattern that keeps your objects in the know when something they might care about happens. Objects can even decide at runtime whether they want to be kept informed.
Observer
2 / 26
Observer
4 / 26
Your job
Create an application that uses the WeatherData object to update three displays for current conditions, weather stats, and a forecast. Your starting point: the WeatherData class.
Observer
5 / 26
Observer
6 / 26
getTemperature() for the temperature; getHumidity() for humidity; and getPressure() for barometric pressure.
The measurementsChanged() method is called any time new weather measurement data is available. We need to implement three display elements that use the weather data: a current conditions display, a statistics display and a forecast display. These displays must be updated each time WeatherData has new measurements. The system must be expandable: other developers can create new custom display elements and users can add or remove as many display elements as they want to the application.
Luca Vigan (Universit di Verona) Observer Lab. Ing. del SW, 06.05.2011 7 / 26
Observer
8 / 26
Brain teaser
Observer
9 / 26
Observer
10 / 26
If you understand newspaper subscriptions, you pretty much understand the Observer Pattern, only we call the publisher the subject and the subscribers the observers.
Observer
11 / 26
Observer
12 / 26
Observer
13 / 26
Observer
14 / 26
Observer
15 / 26
Observer
16 / 26
Observer
17 / 26
Observer
18 / 26
Observer
19 / 26
Observer
20 / 26
Subject interface:
Objects use this interface to register as observers and also to remove themselves from being observers.
Observer interface:
All potential observers need to implement the Observer interface. This interface just has one method, update(), that gets called when the Subjects state changes.
ConcreteSubject class:
A concrete subject always implements the Subject Interface. In addition to the register and remove methods, the concrete subject implements a notifyObservers() method that is used to update all the current observers whenever state changes. The concrete subject may also have methods for setting and getting its state (more about this later).
ConcreteObserver class:
Concrete observers can be any class that implements the Observer interface. Each observer registers with a concrete subject to receive updates.
Luca Vigan (Universit di Verona) Observer Lab. Ing. del SW, 06.05.2011 21 / 26
Questions
What does this have to do with one-to-many relationships? With the Observer pattern, the Subject is the object that contains the state and controls it. So, there is ONE subject with state. The observers, on the other hand, use the state, even if they dont own it. There are many observers and they rely on the Subject to tell them when its state changes. So there is a relationship between the ONE Subject to the MANY Observers. How does dependence come into this? Because the subject is the sole owner of that data, the observers are dependent on the subject to update them when the data changes. This leads to a cleaner OO design than allowing many objects to control the same data.
Observer
22 / 26
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. Why?
The only thing the subject knows about an observer is that it implements a certain interface. We can add new observers at any time. We never need to modify the subject to add new types 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.
Observer
23 / 26
Design Principle Strive for loosely coupled designs between objects that interact. Loosely coupled designs allow us to build exible OO systems that can handle change because they minimize the interdependency between objects.
Observer
24 / 26
Before moving on, try sketching out the classes youll need to implement the Weather Station, including the WeatherData class and its display elements. Make sure your diagram shows how all the pieces t together and also how another developers might implement her own display element. Now you are ready to start your implementation. Java provides some built-in support for the Observer pattern, however, get your hands dirty for now.
Observer
25 / 26
Observer
26 / 26