The Observer pattern
The Observer pattern is often used in event-driven systems to publish events to subscribed objects, usually by calling a method on them. An object that publishes events is called a subject or publisher. Objects that receive events from a publisher are called observers or subscribers. From now on, we will use the terms publisher and subscriber.
A publisher has an internal list of subscribers and provides an interface to register and unregister a subscriber from the internal list. It also provides the notify
method, used by its client, which in turn calls update
methods on subscribers – that’s why we say that the publisher notifies subscribers.
An example of a publisher-subscriber mechanism that is common in embedded systems would be a temperature publisher, which notifies the logger, display, and data sender at regular intervals. Before we go on to the implementation of this example, we will first go through a UML diagram of the Observer pattern...