The Observer Design Pattern is a type of Behavioural Design Pattern that use to get information when certain events happen i.e. basically one component want information about something happening in the other component. And that can a lot of things like a field changes to a particular value or you want to information when the object does a particular thing, etc. Observer Design Pattern in Modern C++ enables you to create subscription mechanism to notify multiple objects about events that happen to the object they’re observing.
By the way, If you haven’t check out my other articles on Behavioural Design Patterns, then here is the list:
- Chain of responsibility
- Command
- Interpreter
- Iterator
- Mediator
- Memento
- Observer
- State
- Strategy
- Template Method
- Visitor
The code snippets you see throughout this series of articles are simplified not sophisticated. So you often see me not using keywords like override
, final
, public
(while inheritance) just to make code compact & consumable(most of the time) in single standard screen size. I also prefer struct
instead of class
just to save line by not writing “public:
” sometimes and also miss virtual destructor, constructor, copy constructor, prefix std::
, deleting dynamic memory, intentionally. I also consider myself a pragmatic person who wants to convey an idea in the simplest way possible rather than the standard way or using Jargons.
Note:
- If you stumbled here directly, then I would suggest you go through What is design pattern? first, even if it is trivial. I believe it will encourage you to explore more on this topic.
- All of this code you encounter in this series of articles are compiled using C++20(though I have used Modern C++ features up to C++17 in most cases). So if you don’t have access to the latest compiler you can use https://wandbox.org/ which has preinstalled boost library as well.
Intent
To get notifications when events happen.
- The Observer Design Pattern split into two parts:
- observer i.e. object which gets a notification about something happening somewhere in the system.
- observable i.e. entity that’s actually generating these notifications or events.
- You see this are the terminology I am using which may vary people-to-people & domain-to-domain, For example:
- event & subscriber
- signal & slot(Boost, Qt, etc.)
- broadcaster & listeners, etc.
Observer Design Pattern Example in C++
|
|
- The observer is that thing which wants to monitor something. And the observable is the component that is to monitored. So, in the above case, our
Person
is observable and the observer isTrafficAdministration
. - You can also augment above code for passing lambda as a subscriber rather than an object for a more functional approach.
Observer Design Pattern with Boost Signals
- Now what I’m going to do is I’ll make a small digression. Because instead of showing you something that we’ve built ourselves what I want to show you is the observable implementation that comes with the Boost libraries.
|
|
- Mind it, I have used
boost::signals2
asboost::signals
are no longer being actively maintained. Due toboost::signals2
, we can get rid ofstd/boost::bind
, and can directly use lambda. You can check out a quick example ofboost::signals2
if you want.
Benefits of Observer Design Pattern
- It supports the loose coupling between objects that interact with each other hence Open-Closed Principle will be intact. Above examples also satisfy the Single Responsibility Principle as Observer & Observable are two different templatized classes which can easily be reusable.
- It provides the flexibility of adding or removing observers at any time which is heavily use in event-driven programming.
Summary by FAQs
Use cases of Observer Design Pattern.
Usually, Observer Design Pattern employs when there is a one-to-many relationship between objects so that when one object changes state, all its dependents are notified and updated automatically. Typical use case area involves GUI libraries, Social media, RSS feeds, Email subscription, etc.
Difference between Observer & Mediator Design Pattern.
Observer Design Pattern works on the one-to-many relationship.
Mediator Design Pattern works on the many-to-many relationship.