data-driven-docs

Selft training repo


Project maintained by ggranados Hosted on GitHub Pages — Theme by mattgraham

The Observer Pattern


Table of Contents


The Observer Pattern is a behavioral design pattern used in software engineering to establish a one-to-many dependency between objects. This pattern defines a relationship where when one object (known as the subject or observable) undergoes a change in its state, all its dependent objects (known as observers) are automatically notified and updated accordingly.

The Observer Pattern is particularly useful in scenarios where multiple objects need to be informed about changes to a certain object’s state, without tightly coupling the objects together. This promotes a more flexible and modular design, allowing changes to one part of the system without affecting other parts.

Back to top

How It Works

img.png

Back to top

Benefits

Back to top

Example

Consider a weather monitoring system. The weather station (subject) gathers weather data and notifies various displays (observers) like a current conditions display, a forecast display, and a statistics display whenever weather conditions change. The displays update themselves based on the received data.

In essence, the Observer Pattern facilitates a publish-subscribe mechanism, enabling objects to communicate without being tightly coupled. It promotes a more maintainable and flexible design by separating concerns and allowing for dynamic interaction between objects.

Back to top

Ref.


Get Started | Design Patterns