Click here to Skip to main content
14,218,216 members

Understanding and Implementing Observer Pattern in C++

Rate this:
4.72 (23 votes)
Please Sign up or sign in to vote.
4.72 (23 votes)
25 Mar 2012CPOL
This article presents the basics of Observer Pattern, when to use it and how to implement it in C++.


This article presents the basics of Observer Pattern, when to use it and how to implement it in C++. I have posted a similar article that talks about the Observer pattern in C#. The main aim of this article will be to implement the observer pattern in C++.


Many a times, we need one part of our application updated with the status of some other part of the application. One way to do this is to have the receiver part repeatedly check the sender for updates but this approach has two main problems. First, it takes up a lot of CPU time to check the new status and second, depending on the interval we are checking for change we might not get the updates "immediately".

This problem has one easy solution, i.e., Observer Pattern. This is my own second article on Observer Pattern. I have a similar article talking about Observer Implementation in C#. I think this article is also worth sharing, as it could be useful for the C++ beginners and also the valuable comments I get on the article will let me learn more.

Here is the class diagram for Observer Pattern(Reference:  GoF Design Patterns)

Using the Code

Let us now discuss all the classes one by one:

  • Subject: This class keeps track of all the observers and provides the facility to add or remove the observers. Also it is the class that is responsible for updating the observers when any change occurs. In our solution, we have ASubject implemented for the same purpose.
  • ConcreteSubject: This class is the real class that implements the Subject. This class is the entity whose change will affect other objects. We have DummyProject class implemented for the same.
  • Observer: This represents an interface that defines the method that should be called whenever there is change. We have implemented this as IObserver.
  • ConcreteObserver: This is the class which needs to keep itself updated with the change. This class just needs to implement the Observer and register itself with the ConcreteSubject and it is all set to receive the updates. We have Shop class in our application serving the same purpose.

The Subject: ASubject

//Header File
#pragma once
#include <vector>
#include <list>
#include "shop.h"

class ASubject
    //Lets keep a track of all the shops we have observing
    std::vector<Shop*> list;

    void Attach(Shop *product);
    void Detach(Shop *product);
    void Notify(float price); 

//CPP File
#include "ASubject.h"
#include <algorithm>

using namespace std;

void ASubject::Attach(Shop *shop)
void ASubject::Detach(Shop *shop)
    list.erase(std::remove(list.begin(), list.end(), shop), list.end());    

void ASubject::Notify(float price)
    for(vector<Shop*>::const_iterator iter = list.begin(); iter != list.end(); ++iter)
        if(*iter != 0)

The ConcreteSubject: DummyProduct

//Header File
#pragma once
#include "ASubject.h"

class DummyProduct : public ASubject
    void ChangePrice(float price);

//CPP File
#include "DummyProduct.h"

void DummyProduct::ChangePrice(float price)

The Observer: IObserver

#pragma once

class IObserver
    virtual void Update(float price) = 0;

The ConcreteObserver: Shop

//Header File
#pragma once
#include <iostream>
#include <string>
#include "IObserver.h"

class Shop : IObserver
    //Name of the Shop
    std::string name;
    float price;
    Shop(std::string n); 
    void Update(float price);          

//CPP File
#include "Shop.h"

Shop::Shop(std::string name)
    this->name = name;

void Shop::Update(float price)
    this->price = price;

    //Lets print on console just to test the working
    std::cout << "Price at "<< name << " is now "<< price << "\n";

Testing the Code

int main(int argc, char* argv[])
    DummyProduct product;
    // We have four shops wanting to keep updated price set by product owner
    Shop shop1("Shop 1");
    Shop shop2("Shop 2");


    //Now lets try changing the products price, this should update the shops automatically

    //Now shop2 is not interested in new prices so they unsubscribe

    //Now lets try changing the products price again

    return 0;

Points of Interest

This article covers the basics of Observer pattern and provides a basic implementation in C++. I have also implemented the same in C#. What I learnt from it is how the observer pattern works and what are the similarities and differences in implementing it in C++ and C#.


  • 10 Feb, 2012: Simple and rudimentary implementation of Observer pattern in C++
  • 26 March 2012: Changed the class diagram.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Rahul Rajat Singh
India India

I Started my Programming career with C++. Later got a chance to develop Windows Form applications using C#. Currently using C#, ASP.NET & ASP.NET MVC to create Information Systems, e-commerce/e-governance Portals and Data driven websites.

My interests involves Programming, Website development and Learning/Teaching subjects related to Computer Science/Information Systems. IMO, C# is the best programming language and I love working with C# and other Microsoft Technologies.

  • Microsoft Certified Technology Specialist (MCTS): Web Applications Development with Microsoft .NET Framework 4
  • Microsoft Certified Technology Specialist (MCTS): Accessing Data with Microsoft .NET Framework 4
  • Microsoft Certified Technology Specialist (MCTS): Windows Communication Foundation Development with Microsoft .NET Framework 4

If you like my articles, please visit my website for more:[^]

  • Microsoft MVP 2015

Comments and Discussions

QuestionGet the arrived item and send it manually Pin
Member 99234865-Jun-17 21:50
memberMember 99234865-Jun-17 21:50 
QuestionQuery regarding Observer Pattern in C++ Pin
Member 1161310020-Apr-15 22:15
memberMember 1161310020-Apr-15 22:15 
AnswerRe: Query regarding Observer Pattern in C++ Pin
Member 1173740223-Mar-17 19:56
memberMember 1173740223-Mar-17 19:56 
GeneralMy vote of 3 Pin
dliviu29-Jan-15 1:43
memberdliviu29-Jan-15 1:43 
QuestionShop price Pin
dliviu29-Jan-15 0:57
memberdliviu29-Jan-15 0:57 
QuestionScaling the Observer Pattern Pin
Member 1139943924-Jan-15 4:54
memberMember 1139943924-Jan-15 4:54 
Questionplz use iterator::;end() Pin
misserwell23-Dec-14 0:00
membermisserwell23-Dec-14 0:00 
QuestionShould the subject know the concrete Observer class? Pin
satishmanohar19-Aug-14 6:43
membersatishmanohar19-Aug-14 6:43 
AnswerRe: Should the subject know the concrete Observer class? Pin
zeeshan_azmi20-Nov-14 2:24
memberzeeshan_azmi20-Nov-14 2:24 
Question[My vote of 2] it isn't thread safe Pin
pj220_20063-Jun-14 23:42
memberpj220_20063-Jun-14 23:42 
GeneralGood skill Pin
hussain.arshad47-May-14 2:59
memberhussain.arshad47-May-14 2:59 
QuestionWhat is the need of class "DummyProduct" Pin
sumit kushwaha13-Feb-14 0:43
membersumit kushwaha13-Feb-14 0:43 
AnswerRe: What is the need of class "DummyProduct" Pin
dliviu29-Jan-15 1:00
memberdliviu29-Jan-15 1:00 
SuggestionRe: What is the need of class "DummyProduct" Pin
C3D19-Apr-15 3:22
professionalC3D19-Apr-15 3:22 
QuestionCompetitions Pin
Dmitri Nеstеruk16-Jun-13 3:53
memberDmitri Nеstеruk16-Jun-13 3:53 
QuestionSeems violating the pattern.... Pin
Musthafa (Member 379898)24-Apr-13 16:44
memberMusthafa (Member 379898)24-Apr-13 16:44 
GeneralVote of 5 Pin
Rahul Warhekar from Pune, MH21-Feb-13 22:43
memberRahul Warhekar from Pune, MH21-Feb-13 22:43 
Really very useful to understand Observer.
GeneralMy vote of 5 Pin
RASPeter23-May-12 9:47
memberRASPeter23-May-12 9:47 
GeneralMy vote of 1 Pin
FerretallicA26-Mar-12 13:20
memberFerretallicA26-Mar-12 13:20 
GeneralMy vote of 1 Pin
zennie19-Mar-12 21:49
memberzennie19-Mar-12 21:49 
SuggestionUsing templates Pin
John Wellbelove13-Feb-12 22:15
memberJohn Wellbelove13-Feb-12 22:15 
GeneralMy vote of 4 Pin
KjellKod.cc13-Feb-12 10:20
memberKjellKod.cc13-Feb-12 10:20 
GeneralRe: My vote of 4 Pin
Stefan_Lang14-Feb-12 0:41
memberStefan_Lang14-Feb-12 0:41 
GeneralRe: My vote of 4 Pin
KjellKod.cc15-Feb-12 3:19
memberKjellKod.cc15-Feb-12 3:19 
GeneralRe: My vote of 4 Pin
Stefan_Lang16-Feb-12 23:12
memberStefan_Lang16-Feb-12 23:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 10 Feb 2012

Tagged as


38 bookmarked