Design Patterns
Observer Pattern
Observer Pattern
• Don’t miss out when something interesting (in your system)
happens!
• The observer pattern allows objects to keep other objects
informed about events occurring within a software system (or
across multiple systems)
• It’s dynamic in that an object can choose to receive or not receive
notifications at run-time
• Observer happens to be one of the most heavily used patterns in
the Java Development Kit
• and indeed is present in many frameworks
Observer Pattern – Example
We need to pull information from a weather station and then generate
“current conditions, weather stats, and a weather forecast”.
Observer Pattern – WeatherData Skeleton
• We receive a partial
implementation of the
WeatherData class from our client.
• They provide three getter methods
for the sensor values and an empty
measurementsChanged() method
that is guaranteed to be called
whenever a sensor provides a new
value
• We need to pass these values to our
three displays… simple!
Observer Pattern – measurementsChanged()
Problems:
1. The number and type of displays may vary. These three displays
are hard coded with no easy way to update them.
2. Coding to implementations, not an interface! Each implementation
has adopted the same interface, so this will make translation easy!
Observer Pattern
• This situation can benefit from use of the observer pattern
• This pattern is similar to subscribing to a newspaper
• A newspaper comes into existence and starts publishing editions
• You become interested in the newspaper and subscribe to it
• Any time an edition becomes available, you are notified (by the fact that it is delivered to
you)
• When you don’t want the paper anymore, you unsubscribe
• The newspaper’s current set of subscribers can change at any time
• Observer is just like this but we call the publisher the “subject” and
we refer to subscribers as “observers”
Observer Pattern – In Action
Observer
1
Subject maintains a list
Subject Observer
of observers
2
Observer
3
Observers
Observer Pattern – In Action
Observer
1
Observer If the Subject changes,
Subject
2 it notifies its observers
Observer
3
Observers
Observer Pattern – In Action
Observer
1
Observer If needed, an observer may
Subject
2 query its subject for more
information
Observer
3
Observers
Observer Pattern – In Action
Observer
1
At any point, an observer may
join or leave the set of observers
Observer
Subject
2
Observer
3
Observer
4
Observers
Observer Pattern
• The Observer Pattern defines a one-to-many dependency between a
set of objects, such that when one object (the subject) changes, all of
its dependents (observers) are notified and updated automatically
Observer Pattern – Benefits
• Observer affords a loosely coupled interaction between subject and
observer
• This means they can interact with very little knowledge about each other
• Consider
• The subject only knows that observers implement the Observer interface
• We can add/remove observers of any type at any time
• We never have to modify subject to add a new type of observer
• We can reuse subjects and observers in other contexts
• The interfaces plug-and-play anywhere observer is used
• Observers may have to know about the ConcreteSubject class if it provides
many different state-related methods
• Otherwise, data can be passed to observers via the update() method