Course Content
Data Structures & Algorithms
Full Stack Web Development
Understanding and playing with DOM (Document Object Model)
0/2
MERN project
0/2
Low Level System Design
LLD Topics
High Level System Design
Fast-Track to Full Spectrum Software Engineering
Observer 

The Observer Pattern is a behavioral design pattern that defines a one-to-many dependency between objects so that when one object changes state, all its dependent objects are automatically notified and updated.



Purpose of the Observer Pattern

The Observer Pattern is used to establish communication between objects, where one object (called the subject) maintains a list of dependents (called observers) and notifies them of any state changes, typically by calling one of their methods.

 

This allows for loose coupling between the subject and observers, enabling systems that are more flexible, dynamic, and easier to maintain.



Real-World Analogy

Think of a newsletter subscription:

 

  • When someone subscribes to a newsletter, they become an observer.
  • The newsletter publisher is the subject.
  • Every time a new edition is released, it is automatically sent to all subscribers.

 

Here, the publisher doesn’t need to know the specifics of each subscriber—it simply broadcasts the update.



Key Components

  • Subject (Publisher) – Holds the state and maintains a list of observers. Notifies observers when the state changes.

 

  • Observer (Subscriber) – Wants to be informed of changes in the subject.

 

  • ConcreteSubject – A specific implementation of the subject.

 

  • ConcreteObserver – A specific implementation of the observer that reacts to updates.


How It Works

  • Observers register with a subject to receive updates.
  • When the subject’s state changes, it notifies all registered observers.
  • Observers can then update themselves based on the change.

 

This process allows for automatic synchronization between the subject and its dependents.



Common Use Cases

  • User Interface systems – Automatically updating UI components when data changes.

 

  • Event listeners in software – E.g., button click listeners.

 

  • Stock market systems – Updating clients in real-time when stock prices change.

 

  • Social media notifications – Followers get updates when someone posts.

 

  • Chat applications – Real-time message updates in group chats.


Advantages of the Observer Pattern

  • Loose coupling – Subjects and observers can evolve independently.
  • Dynamic relationships – Observers can be added or removed at runtime.
  • Broadcast communication – One subject can update many observers at once.
  • Enhances reusability – Generic observer interfaces allow for reuse across different systems.


Limitations

  • Memory leaks – If observers are not removed properly, it can cause resource issues.

 

  • Unexpected updates – Observers might receive frequent notifications and need filtering.

 

  • Order of notification – Observers may be notified in an unpredictable order unless explicitly managed.


Summary

The Observer Pattern is ideal for event-driven and real-time systems where components need to stay in sync. It allows changes in one object to be automatically propagated to many others without tight dependencies, fostering a more modular and maintainable design.

0% Complete
WhatsApp Icon

Hi Instagram Fam!
Get a FREE Cheat Sheet on System Design.

Hi LinkedIn Fam!
Get a FREE Cheat Sheet on System Design

Loved Our YouTube Videos? Get a FREE Cheat Sheet on System Design.