The GoF Observer pattern
The GoF Observer pattern is also referred to as a publish-subscribe pattern in the GoF book. The idea is simple. EventSource
(classes that emits events) will have a one-to-many relationship with event sinks (classes that listen to the event notification). Every EventSource
will have a mechanism for the event sinks to subscribe to, in order to get different types of notification. A single EventSource
might emit multiple events. An EventSource
can send notifications to thousands of subscribers (event sinks or listeners) when there is a change in state or something significant happens in its realm. The EventSource
will walk through the list of subscribers and notify them one by one. The GoF book was written at a time when the world was mostly doing sequential programming. Topics such as concurrency were mostly related to platform-specific libraries or the POSIX
thread library. We will write a simple C++ program to demonstrate the whole idea of the Observer pattern....