Software Design & Architecture
Software Design & Architecture
(SDA)
DESIGN PATTERNS
2
Behavioral Patterns
3
Observer Design Pattern
It is a behavioral design pattern that lets you define a subscription
mechanism to notify multiple objects about any events that happen to the
object they’re observing.
4
15/22
Observer Design Pattern
Used when there is one-to-many relationship
between objects such as if one object is modified,
its dependent objects are to be notified
automatically
to keep a set of objects up to date with the state of a
designated object
It primarily deals with the interaction and communication between objects,
specifically focusing on how objects behave in response to changes in the state
of other objects.
The pattern is concerned with defining a mechanism for a group of objects to
interact based on changes in the state of one object (the subject).
It encapsulates the behavior of the dependent objects (observers) and
allows for a clean separation between the subject and its observers. This
separation promotes a more modular and maintainable design.
The pattern promotes loose coupling between the subject and its observers.
The subject doesn’t need to know the concrete classes of its observers, and
observers can be added or removed without affecting the subject.
The primary mechanism in the Observer Pattern is the notification of
observers when a change occurs. This notification mechanism facilitates the
dynamic and coordinated behavior of multiple objects in response to changes
in the subject.
6
Real-world analogy of the Observer Design Pattern
Let us Imagine a scenario where the weather station is observed by
various smart devices. The weather station maintains a list of registered
devices. When there’s a change in weather conditions, the weather station
notifies all devices about the update.
7
Components of Observer Design Pattern
1. Subject: Maintains a list of observers (subscribers or listeners). It Provides methods
to register and unregister observers dynamically and defines a method to notify
observers of changes in its state.
2. Observer: Observer defines an interface with an update method that concrete
observers must implement and ensures a common or consistent way for concrete
observers to receive updates from the subject. Concrete observers implement this
interface, allowing them to react to changes in the subject’s state.
3. ConcreteSubject: ConcreteSubjects are specific implementations of the subject.
They hold the actual state or data that observers want to track. When this state
changes, concrete subjects notify their observers. For instance, if a weather station
is the subject, specific weather stations in different locations would be concrete
subjects.
4. ConcreteObserver: Concrete Observer implements the observer interface. They
register with a concrete subject and react when notified of a state change. When the
subject’s state changes, the concrete observer’s update() method is invoked,
allowing it to take appropriate actions. In a practical example, a weather app on your
smartphone is a concrete observer that reacts to changes from a weather station. 8
Consider a scenario where you have a weather monitoring system.
Different parts of your application need to be updated when the weather
conditions change.
9
Challenges
Challenges or difficulties while implementing this system without Observer
Design Pattern
Components interested in weather updates would need direct references
to the weather monitoring system, leading to tight coupling.
Adding or removing components that react to weather changes requires
modifying the core weather monitoring system code, making it hard to
maintain.
How Observer Pattern helps to solve above challenges?
The Observer Pattern helps decouple the weather monitoring system
from the components interested in weather updates. Each component
can register as an observer, and when the weather changes, the
observers are notified. This way, adding or removing components doesn’t
affect the weather monitoring system.
10
Class Diagram of Observer
11
Observer Design Pattern
Summary of Behavioral Patterns
Behavioral Design Patterns capture behavior among objects
Iterator visits members of a collection
Mediator captures behavior among peer objects
Observer updates objects affected by a single object
State allows method behavior to depend on current status
Chain of Responsibility allows a set of objects to provide
functionality collectively
Command captures function flexibly (e.g. undo-able)
Template captures basic algorithms, allowing variability
References
Chapter 7 : “Software Engineering Design Theory and Practice”
https://www.tutorialspoint.com/design_pattern/template_pattern.htm
https://sourcemaking.com/design_patterns/iterator
14
15