ObserverPattern
COMP2511
Observer Pattern
Prepared by
Dr. Ashesh Mahidadia
Observer Pattern
These lecture notes use material from the wikipedia page at: https://en.wikipedia.org/wiki/Observer_pattern
and
the reference book “Head First Design Patterns”.
2COMP2511: Observer Pattern
Observer Pattern
• The Observer Pattern is used to implement distributed event handling systems, in
“event driven” programming.
• In the observer pattern
• an object, called the subject (or observable or publisher ) , maintains a list of its
dependents, called observers (or subscribers), and
• notifies the observers automatically of any state changes in the subject, usually by
calling one of their methods.
• Many programming languages support the observer pattern,
Graphical User Interface libraries use the observer pattern extensively.
COMP2511: Observer Pattern 3
Observer Pattern
• The Observer Pattern defines a one-to-many dependency between objects so that
when one object (subject) changes state, all of its dependents (observers) are
notified and updated automatically.
• The aim should be to,
• define a one-to-many dependency between objects withoutmaking the objects
tightly coupled.
• automatically notify/update an open-ended number of observers (dependent
objects) when the subject changes state
• be able to dynamically add and remove observers
COMP2511: Observer Pattern 4
Observer Pattern: Possible Solution
• Define Subject and Observer interfaces, such that when a subject changes state, all
registered observers are notified and updated automatically.
• The responsibility of,
• a subject is to maintain a list of observers and to notify them of
state changes by calling their update() operation.
• observers is to register (and unregister) themselves on a subject (to get
notified of state changes) and to update their state when they are notified.
• This makes subject and observers loosely coupled.
• Observers can be added and removed independently at run-time.
• This notification-registration interaction is also known as publish-subscribe.
COMP2511: Observer Pattern 5
Java Observer and Observable : Deprecated
The following java library classes have been deprecated in Java 9 because the model
implemented was quite limited.
• java.util.Observer and
• java.util.Observable
Limitations
• Observable is a class, not an interface !
• Observable protects crucial methods, the setChanged() method is protected.
• we can’t call setChanged() unless we subclass Observable! Inheritance is must, bad
design J
• we can’t add on the Observable behavior to an existing class that already extends
another superclass.
• there isn’t an Observable interface, for a proper custom implementation
COMP2511: Observer Pattern 6
COMP2511: Observer Pattern 7
Observer-1
Observer-2
Observer-n
Thermometer
(subject)
Change propagation
Hydrometer
(subject)
Observer-3
Observer-4
Change propagation
Multiple Observers and Subjects
Observers / Subscribers / Listeners Observables / Subjects / Publishers
COMP2511: Observer Pattern
8
Observer Pattern: Possible Solution
Read the example code
discussed/developed in the lectures,
and also provided for this week
Passing data: Push or Pull
The Subject needs to pass (change) data while notifying a change to an Observer. Two
possible options,
Push data
• Subject passes the changed data to its observers, for example:
update(data1,data2,…)
• All observersmust implement the above update method.
Pull data
• Subject passes reference to itself to its observers, and the observers need
to get (pull) the required data from the subject, for example:
update(this)
• Subject needs to provide the required access methods for its observers.
For example, public double getTemperature() ;
COMP2511: Observer Pattern 9
COMP2511: Observer Pattern 10
Read the example code
discussed/developed in the lectures,
and also provided for this week
Notify Observers
after every update
COMP2511: Observer Pattern 11
Read the example code
discussed/developed in the lectures,
and also provided for this week
Display after an update
Update for
Multiple Subjects
COMP2511: Observer Pattern 12
remove
add / register
change state
Demos …
• Live Demos …
• Make sure you properly understand the demo example code
available for this week.
COMP2511: Observer Pattern 13
Observer Pattern: Example
The above image is from https://www.oodesign.com/observer-pattern.html
COMP2511: Observer Pattern 14
Observer Pattern: UI Example
COMP2511: Observer Pattern 15
Summary
Advantages:
• Avoids tight coupling between Subject and its Observers.
• This allows the Subject and its Observers to be at different levels of abstractions
in a system.
• Loosely coupled objects are easier to maintain and reuse.
• Allows dynamic registration and deregistration.
Be careful:
• A change in the subject may result in a chain of updates to its observers and in
turn their dependent objects – resulting in a complex update behaviour.
• Need to properly manage such dependencies.
COMP2511: Observer Pattern 16
Summary
COMP2511: Observer Pattern 17
From the reference book: “Head First Design Pattern”