0% found this document useful (0 votes)
37 views26 pages

Observer Pattern: Luca Viganò

The document describes the Observer design pattern, which allows objects called observers to subscribe to and receive notifications from another object called a subject when the subject's internal state changes. This pattern establishes a one-to-many dependency between the subject and observers, so that when the subject's state changes, all of its observers are notified and updated automatically. The document uses the example of a weather monitoring application to illustrate how to implement the Observer pattern, with a WeatherData subject notifying display element observers of weather measurement changes.

Uploaded by

api-251901021
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views26 pages

Observer Pattern: Luca Viganò

The document describes the Observer design pattern, which allows objects called observers to subscribe to and receive notifications from another object called a subject when the subject's internal state changes. This pattern establishes a one-to-many dependency between the subject and observers, so that when the subject's state changes, all of its observers are notified and updated automatically. The document uses the example of a weather monitoring application to illustrate how to implement the Observer pattern, with a WeatherData subject notifying display element observers of weather measurement changes.

Uploaded by

api-251901021
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Observer Pattern

Luca Vigan
Dipartimento di Informatica Universit di Verona

Laboratorio di Ingegneria del Software 06.05.2011

Luca Vigan (Universit di Verona)

Observer

Lab. Ing. del SW, 06.05.2011

1 / 26

Keeping your Objects in the Know (The Observer Pattern)

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.

Luca Vigan (Universit di Verona)

Observer

Lab. Ing. del SW, 06.05.2011

2 / 26

A Problem Description: Weather Monitoring


Weather-O-Rama has just selected your company to build a next generation Internet-based Weather Monitoring Station. This is what Weather-O-Rama writes: The weather station will be based on our patent pending WeatherData object, which tracks current weather conditions (temperature, humidity, and barometric pressure). Wed like for you to create an application that initially provides three display elements: current conditions, weather statistics and a simple forecast, all updated in real time as the WeatherData object acquires the most recent measurements. Further, this is an expandable weather station. Weather-O-Rama wants to release an API so that other developers can write their own weather display and plug them right in. Wed like for you to supply that API!
Luca Vigan (Universit di Verona) Observer Lab. Ing. del SW, 06.05.2011 3 / 26

The Weather Monitoring application overview


The three players in the system are:
the weather station (the physical device that acquires the actual weather data), the WeatherData object (that tracks the data coming from the weather station and updates the display), the display that shows users the current weather conditions.

Luca Vigan (Universit di Verona)

Observer

Lab. Ing. del SW, 06.05.2011

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.

Luca Vigan (Universit di Verona)

Observer

Lab. Ing. del SW, 06.05.2011

5 / 26

The WeatherData class

Luca Vigan (Universit di Verona)

Observer

Lab. Ing. del SW, 06.05.2011

6 / 26

What do we know so far?


The WeatherData class has getter methods for three measurement values:
1 2 3

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

A rst, misguided wild guess at the Weather Station


Here is a rst implementation possibility:

Luca Vigan (Universit di Verona)

Observer

Lab. Ing. del SW, 06.05.2011

8 / 26

Brain teaser

Based on our rst implementation, which of the following apply?


a) b) c) d) e) f) we are coding to concrete implementations, 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 dont implement a common interface we havent encapsulated the part that changes we are violating encapsulation of the WeatherData class

Solution: a, c, e and partly b.

Luca Vigan (Universit di Verona)

Observer

Lab. Ing. del SW, 06.05.2011

9 / 26

Whats wrong with our implementation?


Think back to all those concepts and principles...

Luca Vigan (Universit di Verona)

Observer

Lab. Ing. del SW, 06.05.2011

10 / 26

Meet the Observer Pattern


How newspaper or magazine subscriptions work:
1. A newspaper publisher goes into business and begins publishing newspapers. 2. You subscribe to a particular publisher, and every time theres a new edition it gets delivered to you. As long as you remain a subscriber, you get new newspapers. 3. You unsubscribe when you dont want papers anymore, and they stop being delivered. 4. While the publisher remains in business, people, hotels, airlines and other business constantly subscribe and unsubscribe to the newspaper.

If you understand newspaper subscriptions, you pretty much understand the Observer Pattern, only we call the publisher the subject and the subscribers the observers.

Luca Vigan (Universit di Verona)

Observer

Lab. Ing. del SW, 06.05.2011

11 / 26

Publisher + Subscribers = Observer Pattern

Luca Vigan (Universit di Verona)

Observer

Lab. Ing. del SW, 06.05.2011

12 / 26

Publisher + Subscribers = Observer Pattern


A Duck object comes along and tells the Subject that it wants to become an observer:

Luca Vigan (Universit di Verona)

Observer

Lab. Ing. del SW, 06.05.2011

13 / 26

Publisher + Subscribers = Observer Pattern


The Duck object is now an ofcial observer:

Luca Vigan (Universit di Verona)

Observer

Lab. Ing. del SW, 06.05.2011

14 / 26

Publisher + Subscribers = Observer Pattern


The Subject gets a new data value!

Luca Vigan (Universit di Verona)

Observer

Lab. Ing. del SW, 06.05.2011

15 / 26

Publisher + Subscribers = Observer Pattern


The Mouse object asks to be removed as an observer:

Luca Vigan (Universit di Verona)

Observer

Lab. Ing. del SW, 06.05.2011

16 / 26

Publisher + Subscribers = Observer Pattern


Mouse is outta here!

Luca Vigan (Universit di Verona)

Observer

Lab. Ing. del SW, 06.05.2011

17 / 26

Publisher + Subscribers = Observer Pattern


The Subject has another new int:

Luca Vigan (Universit di Verona)

Observer

Lab. Ing. del SW, 06.05.2011

18 / 26

The Observer Pattern dened


The Observer Pattern denes a one-to-many dependency between objects so that when one object changes state, all of its dependents are notied and updated automatically:

Luca Vigan (Universit di Verona)

Observer

Lab. Ing. del SW, 06.05.2011

19 / 26

The Observer Pattern dened: the class diagram

Luca Vigan (Universit di Verona)

Observer

Lab. Ing. del SW, 06.05.2011

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.

Luca Vigan (Universit di Verona)

Observer

Lab. Ing. del SW, 06.05.2011

22 / 26

The power of Loose Coupling

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.

Luca Vigan (Universit di Verona)

Observer

Lab. Ing. del SW, 06.05.2011

23 / 26

The power of Loose Coupling

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.

Luca Vigan (Universit di Verona)

Observer

Lab. Ing. del SW, 06.05.2011

24 / 26

Implementing the Weather Station

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.

Luca Vigan (Universit di Verona)

Observer

Lab. Ing. del SW, 06.05.2011

25 / 26

Luca Vigan (Universit di Verona)

Observer

Lab. Ing. del SW, 06.05.2011

26 / 26

You might also like