SYSTEM DESIGN
OBSERVER
DESIGN
PATTERN
Rohan Thapa
thaparohan2019@[Link]
What is the Observer Pattern?
The Observer Pattern is a behavioral design
pattern where an object, known as the
subject, maintains a list of its dependent
objects, called observers.
The subject automatically notifies all
registered observers of any changes to its
state, usually by calling one of their methods.
This pattern is particularly useful in event-
driven systems and promotes loose coupling
between objects.
Key Concepts of Observer
Pattern
Subject: The object being observed. It
holds a reference to all its observers and
notifies them of any changes.
Observer: The dependent objects that get
updated when the subject's state changes.
Loose Coupling: The subject and observers
are loosely coupled, meaning they interact
through an interface, not through concrete
implementations. This makes the system
more flexible and maintainable.
Real-Time Updates: All observers get the
updated state as soon as the subject
changes its internal state.
Real-World Use Cases:
Stock Market Applications: Stock market
applications often use the Observer
Pattern to notify registered users when
stock prices change.
News Feeds/Subscription Systems: Users
can subscribe to various topics (subjects)
and get notifications when new articles are
published.
GUI Event Listeners: In graphical user
interfaces, actions like button clicks notify
multiple event handlers (observers).
Code Example: Weather
Station
Imagine a Weather Station (subject) that
tracks temperature and notifies multiple
display units (observers), like a current
conditions display and statistics display.
1. Subject Interface (WeatherStation)
The subject holds a list of observers and
provides methods to register, remove, and
notify them.
Code Example:
Code Example:
2. Observer Interface and Concrete Observers
(Display Units)
Each observer implements the Observer
interface and the update() method to receive
updates from the subject.
Code Example:
Code Example:
3. Using the Observer Pattern
Here’s how the weather station interacts with
the display units.
A WeatherStation (subject) notifies multiple display units
(observers) like CurrentConditionsDisplay and
StatisticsDisplay whenever the temperature changes.
Why Use the Observer
Pattern?
1. Loose Coupling: The subject doesn’t know
anything about the concrete observers; it
only knows that it needs to notify them.
This makes it easier to extend and maintain
the system.
2. Scalability: You can easily add more
observers without modifying the subject's
code. For example, adding a new type of
display is as simple as creating another
observer.
3. Real-Time Systems: Perfect for
applications that require real-time
updates, such as live stock tickers or chat
applications.
Questions
What is the Observer Pattern?
Answer: The Observer Pattern is a behavioral
design pattern where an object (subject)
maintains a list of dependents (observers) and
notifies them of any state changes, allowing
for loose coupling between objects.
How does the Observer Pattern promote
loose coupling?
Answer: The subject and observers
communicate through interfaces, not
concrete implementations. This allows the
subject to function without knowing specific
details about its observers, promoting
flexibility and maintainability.
Questions
When would you use the Observer Pattern?
Answer: You would use the Observer Pattern
when one object’s state changes need to be
reflected in several dependent objects. It is
useful in event-driven or notification-based
systems like GUIs, stock market apps, or
messaging systems.
Can you name some real-world examples of
the Observer Pattern?Answer: Yes, common
real-world examples include notification
systems (like email alerts), stock market
applications that notify users of price
changes, and event listeners in GUIs.
Summary
The Observer Pattern is a great tool when you
need multiple objects to react to changes in
another object's state. It’s a highly flexible
pattern used in real-time systems like stock
market apps, notification systems, and even
GUI-based programs. By maintaining loose
coupling between the subject and its
observers, you can easily scale and maintain
your applications.
Thank You
Rohan Thapa
thaparohan2019@[Link]