Robert Larson

  • Home
  • About Me
  • Contact Me
  • Product Recommendations
  • Client Portal

Observer Pattern using C++

Observer Pattern using C++

February 9, 2017 by Robert Larson

Table of Contents

  • What is the Observer Pattern?
  • What are the benefits of the Observer Pattern?
  • Example of the Observer Pattern using C++
  • The Observer Interface : BlogObserver
  • The Subject Interface: BlogSubject
  • The Subject : MyBlog
  • The Observers
  • The Observer Pattern in Action
  • Recommended Resources

What is the Observer Pattern?

The Observer Pattern is a design pattern that allows any number of objects to observe state of a single object and be informed of any changes. The single object is typically described as the subject where the objects interested in changes of the subject are known as observers. The observers could be notified that were was a change and query the subject or be updated with what the change was.

What are the benefits of the Observer Pattern?

There are a few benefits of the Observer Pattern. First, it provides a nice objected oriented design by keeping the control of the subject’s data within the subject object itself versus allowing many objects (observers) have control over the same data. Second, the pattern, if implemented correctly, should have interfaces to the subject and the observers. This promotes a loose coupling. The subject would depend on the observer’s interface not the respective implementations. Loose coupling is a standard design principle which has several benefits of its own. For example, you can add any number of new observers without having to change the subject implementation. And as long as the subject and observer fulfill there responsibilities of their respective interfaces, changes to one won’t affect the other.

Example of the Observer Pattern using C++

A blog and its subscribers is a good example of where the observer pattern can be used. Consider the content of a blog as being the subject. Typically, blogs allow readers to subscribe to be informed when there is new content on a blog. These readers would be considered your observers. I have written a C++ program where I have three different users (observers) that can subscribe to get blog (subject) updates to show the Observer Pattern in action.

The Observer Interface : BlogObserver

The BlogObserver is an interface that defines one method, BlogUpdate(), for derived objects to implement.

BlogObserver.h

The Subject Interface: BlogSubject

The BlogSubject is an interface that defines methods that allow a BlogObserver to Subscribe or Unsubscribe to receive BlogSubject’s updates. In addition, the Observers are able to request notification of current state via Notify().

BlogSubject.h

The Subject : MyBlog

I have written MyBlog to implement the BlogSubject interface. It contains a BlogData member object as the data subscribed observers would like to be notified about when it changes. The BlogData object implements a StringData interface so its observers depend on the interface versus the concrete class.

MyBlog.h

MyBlog.cpp

The Observers

I have written three different observers. Boss, CoWorker and Friend. Each implement the BlogObserver interface. Notice that each observer depends on the concrete subject object, MyBlog. This is so the observer can get the specific state data the subject contains when notified via BlogUpdate() that the state data has changed.

Boss.h

Boss.cpp

CoWorker.h

CoWorker.cpp

Friend.h

Friend.cpp

The Observer Pattern in Action

So lets take a look at the program that makes use of these objects. First, we want to create a MyBlog object. We use this object to construct our three observers, Boss, CoWorker and Friend. Now we can Subscribe or Unsubscribe our observers with the MyBlog object. You will see that when observers are Subscribed, they are informed when the BlogData changes in MyBlog via SetData().

MyBlogTest.cpp

Here is the output

Boss received "My first blog"
CoWorker received "My first blog"
CoWorker received "My second blog"
Friend received "My second blog"
Friend received "My third blog"

I hope you found this post to be helpful and informative. Please feel free to leave a comment with further discussion, feedback and questions.

Recommended Resources

Here are some resources I recommend for more information on the Observer Pattern

Head First Design Patterns: A Brain-Friendly Guide

Design Patterns: Elements of Reusable Object-Oriented Software

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

Posted in: c++, Design Patterns Tagged: c++, designpatterns
← Remembering My First Scrum Team Common Area Experience.
The Decorator Pattern using C++ →
  • Home
  • About Me
  • Contact Me
  • Product Recommendations
  • Client Portal

Recent Posts

  • March Madness 2019 – Weighted Random Bracket Generator
  • Gifts Ideas for Software Developers 2018-2019
  • March Madness 2018 – Second Chance Weighted Random Bracket Generator
  • March Madness 2018 – Weighted Random Bracket Generator
  • Builder Pattern using C++ (NFL Scheduler Example)

Archives

  • March 2019
  • November 2018
  • March 2018
  • August 2017
  • June 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017

Follow me on Twitter

My Tweets

Copyright © 2025 Robert Larson.

Grace WordPress Theme by SiteChurch

%d