Skip to content

Latest commit

 

History

History

observer

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

< Back To Design Patterns

Observer Design Pattern

Please support my repo with your star.

Definition

The Observer Design Pattern is a behavioral pattern where an object, known as the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes. This pattern is commonly used when the state of one object needs to be reflected in multiple other objects without tightly coupling them together. The observer pattern ensures that the observers are updated whenever the subject undergoes any change, typically using a push notification mechanism.

Example in Real World/Nature Inspired

  1. Weather Forecasting: In a weather station, the weather data (e.g., temperature, humidity) is the subject, and various devices like thermometers, mobile apps, or websites are the observers. Whenever the weather data changes, the devices are automatically updated.
  2. Social Media Notifications: When a person posts a new status on a platform like Facebook, all their followers (observers) are notified of the new post. The user (subject) is the source, and the followers are the observers.
  3. Parent and Child Communication: In a family, a parent (subject) may announce that it’s time to go out. The children (observers) are informed of this change and react accordingly, like getting ready.
  4. Stock Market Alerts: When stock prices change, various trading platforms and individuals (observers) are updated in real-time about the changes in stock values, ensuring they can act quickly.

Real Usage/System Design Problems

  1. UI Event Handling: In a graphical user interface (GUI), the Observer pattern is often used to handle events such as button clicks or user inputs. When the user interacts with an element (subject), all associated listeners (observers) are notified and can respond accordingly.
  2. Real-time Systems: Systems like financial dashboards or real-time monitoring applications require immediate updates when a key value or state changes. The Observer pattern allows these systems to efficiently notify multiple components about the change without them constantly polling for updates.
  3. Event-driven Architecture: In distributed systems, the Observer pattern can be used to decouple services. For example, when a particular event happens in one service (such as an order being placed), other services (like inventory or shipping) are automatically notified, and they react based on the event.

Important Points of Implementation

  • Loose Coupling: The subject and observers should be loosely coupled. The subject does not need to know anything about the specific observers beyond the fact that they implement a certain interface.
  • Efficiency: In some cases, updating all observers can become costly if there are many. It is important to ensure that only necessary observers are notified and that the updates are efficient.
  • Observer Removal: Observers should be able to unregister themselves when they no longer need updates from the subject, to avoid memory leaks or unnecessary processing.
  • Multiple Observers: The Observer pattern allows one subject to have multiple observers, enabling a one-to-many dependency relationship.

< Back To Design Patterns