Discovering Observers - Part 1 -- Sandor Dargo
The goal of this mini-series is to explore the Observer Design Pattern in C++, walking through different implementations and weighing their pros and cons.
Discovering Observers - Part 1
by Sandor Dargo
From the article:
First, let’s briefly recap what the observer pattern is. It belongs to the family of behavioral design patterns.
As a reminder: design patterns are usually grouped into three categories: creational, structural, and behavioral.
You might also have encountered the observer under other names such as listener, event subscriber, or publisher-subscriber.
The central idea is simple: instead of repeatedly querying an object for information, the querying object (the observer) gets notified automatically when the information holder (the subject) changes. For example, imagine an orchestrator object that needs the latest value of a user setting. Instead of polling the setting every
n
milliseconds, it can subscribe to value changes and receive notifications whenever a new value is set.Using the common terminology: there is typically one publisher and one or more subscribers. Subscribers register for events or changes, and whenever an update happens, the publisher notifies them.