Author image

Observer Design Pattern


Difficulty:
1/5


Observer is a very important design pattern which is being used in almost every single self-respecting codebase as a way to efficienty communicate between objects in an event-based function. It wouldn't be an understatement to say it is the foundation of event based programming.

Underlying the popular, ever-present in networking, “Model View Controller” (MVC) design pattern, is the observer pattern.

Observers are used in every single game engine - they're known there mostly as “Event dispatchers”. This includes Unreal Engine.

The pattern promotes loose coupling since the Subjects don't need to know anything about the observers.

Alternative terminology for this pattern includes the following: - Subscribe-Publish - Listener-Publisher - Delegate-Model - Sink-Source, where the Subject is the source of events and the Observers are sinks of events

Design

  • a class, called the Subject, maintains a list of Observers and notifies them automatically of any state changes, usually by calling one of their methods - eg. notify().
  • Various nosy Observer (concrete) classes want to know when the Subject does something in which they are interested in and want to react to it in a potentially unique way. Those classes inherit from the IObserver interface (pure virtual class).
  • Observers subscribe to the Subject class upon their construction.
    • In code: They are given (dependency injection) a pointer to the Subject, they pass it to the base IObserver class's constructor, and then they call the Subject's subscribe method with this as the argument.
  • New subscribers are added to the observer's list.
  • When the subject's state, or a subset of its state that a particular observer is interested in, is changed the Subject notifies all the interested parties on this state change, by calling one of their public methods (typically called notify).
  • Observer objects should automatically unsubscribe themselves from the Subject when destroyed.

Improvements:

An update to this system, which I don't include in this code, is “Selective notification of Observers”. Meaning that an Observer usually subscribes to a particular notification it is interested in, not to any and all actions that the Subject performs (it's typically not interested in everything). The misc.h header file contains various enums/flags which can be used to improve upon.

In reference to our example, the LeftObserver should only be interested when the Subject - Car turns left and the RightObserver when the Car turns right. So the Car should not notify all m_pObservers all the time, rather only the ones that have subscribed to a particular action - selected by a flag (enum class) which indicates what an Observer is interested in.

Tip: for “multiple interests” the observers can pass along a bitwise OR of multiple flags to subscribe to multiple notifications at once.

I used Windows 8.1 x86_64, Visual Studio 2017, Modern C++17 to build the project. It should work on other platforms as well.

Github

Github repository link.


0 likes