🌱Aadam's Garden

Search

Search IconIcon to open search

Observer Pattern

Last updated Jul 13, 2022

Source:: Software Design Patterns - Best Practices for Software Developers


Definition

The pattern is formally defined as a one to many dependency between objects so that when one object changes state all the dependents are notified.

With the observer pattern, we have:

  1. An observable object, which can be observed by subscribers in order to notify them.
  2. Subscribers, which can subscribe to and get notified by the observable object.

For example, we can add the logger as a subscriber to the observable.

When the notify method is invoked on the observable, all subscribers get invoked and (optionally) pass the data from the notifier to them.

Observer Pattern.png
The observers listen to events from the subject and are notified whenever an event occurs. They can then process it accordingly.

# Tradeoffs

# Resources