Click here to Skip to main content
15,889,795 members
Articles / Programming Languages / C++

Understanding and Implementing Observer Pattern in C++

Rate me:
Please Sign up or sign in to vote.
4.72/5 (23 votes)
25 Mar 2012CPOL2 min read 224.4K   1.6K   39   29
This article presents the basics of Observer Pattern, when to use it and how to implement it in C++.

Introduction

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++.

Background

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)

Image 1

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

C++
//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;

public:
    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)
{
    list.push_back(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)
        {
            (*iter)->Update(price);
        }
    }
}

The ConcreteSubject: DummyProduct

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

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

//CPP File
#include "DummyProduct.h"

void DummyProduct::ChangePrice(float price)
{
    Notify(price);
}

The Observer: IObserver

C++
#pragma once

class IObserver
{
public:
    virtual void Update(float price) = 0;
};

The ConcreteObserver: Shop

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

class Shop : IObserver
{
    //Name of the Shop
    std::string name;
    float price;
public:
    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

C++
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");

    product.Attach(&shop1);
    product.Attach(&shop2);

    //Now lets try changing the products price, this should update the shops automatically
    product.ChangePrice(23.0f);

    //Now shop2 is not interested in new prices so they unsubscribe
    product.Detach(&shop2);            

    //Now lets try changing the products price again
    product.ChangePrice(26.0f);

    getchar();
    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#.

History

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

License

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


Written By
Architect
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: www.rahulrajatsingh.com[^]

  • Microsoft MVP 2015

Comments and Discussions

 
QuestionGet the arrived item and send it manually Pin
Member 99234865-Jun-17 21:50
Member 99234865-Jun-17 21:50 
QuestionQuery regarding Observer Pattern in C++ Pin
Member 1161310020-Apr-15 22:15
Member 1161310020-Apr-15 22:15 
AnswerRe: Query regarding Observer Pattern in C++ Pin
Member 1173740223-Mar-17 19:56
Member 1173740223-Mar-17 19:56 
GeneralMy vote of 3 Pin
dliviu29-Jan-15 1:43
dliviu29-Jan-15 1:43 
QuestionShop price Pin
dliviu29-Jan-15 0:57
dliviu29-Jan-15 0:57 
QuestionScaling the Observer Pattern Pin
Member 1139943924-Jan-15 4:54
Member 1139943924-Jan-15 4:54 
Questionplz use iterator::;end() Pin
misserwell23-Dec-14 0:00
misserwell23-Dec-14 0:00 
QuestionShould the subject know the concrete Observer class? Pin
satishmanohar19-Aug-14 6:43
satishmanohar19-Aug-14 6:43 
AnswerRe: Should the subject know the concrete Observer class? Pin
zeeshan_azmi20-Nov-14 2:24
zeeshan_azmi20-Nov-14 2:24 
Question[My vote of 2] it isn't thread safe Pin
pj220_20063-Jun-14 23:42
pj220_20063-Jun-14 23:42 
GeneralGood skill Pin
hussain.arshad47-May-14 2:59
hussain.arshad47-May-14 2:59 
QuestionWhat is the need of class "DummyProduct" Pin
sumit kushwaha13-Feb-14 0:43
sumit kushwaha13-Feb-14 0:43 
AnswerRe: What is the need of class "DummyProduct" Pin
dliviu29-Jan-15 1:00
dliviu29-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
Dmitri Nеstеruk16-Jun-13 3:53 
QuestionSeems violating the pattern.... Pin
Musthafa (Member 379898)24-Apr-13 16:44
Musthafa (Member 379898)24-Apr-13 16:44 
GeneralVote of 5 Pin
Rahul Warhekar from Pune, MH21-Feb-13 22:43
Rahul Warhekar from Pune, MH21-Feb-13 22:43 
GeneralMy vote of 5 Pin
RASPeter23-May-12 9:47
professionalRASPeter23-May-12 9:47 
GeneralMy vote of 1 Pin
FerretallicA26-Mar-12 13:20
FerretallicA26-Mar-12 13:20 
GeneralMy vote of 1 Pin
zennie19-Mar-12 21:49
zennie19-Mar-12 21:49 
SuggestionUsing templates Pin
John Wellbelove13-Feb-12 22:15
John Wellbelove13-Feb-12 22:15 
GeneralMy vote of 4 Pin
KjellKod.cc13-Feb-12 10:20
KjellKod.cc13-Feb-12 10:20 
GeneralRe: My vote of 4 Pin
Stefan_Lang14-Feb-12 0:41
Stefan_Lang14-Feb-12 0:41 
GeneralRe: My vote of 4 Pin
KjellKod.cc15-Feb-12 3:19
KjellKod.cc15-Feb-12 3:19 
GeneralRe: My vote of 4 Pin
Stefan_Lang16-Feb-12 23:12
Stefan_Lang16-Feb-12 23:12 
I've looked over signals and slots, and found them to be not that different from the observer pattern. The main difference seems to be that it separates the logic (i. e. who signals what to whom) is separated from the observers.

I can see why that would make sense in some systems, but from an OO point of view, most objects that I can think of in the role of an observer 'know' very well what signals they should listen for, and therefore don't need that kind of separation. You may argue that an observer may not know what types of signals there may be out there, and therefore they may not know all the signals they should react to - but if that is the case, then how should the observer know how to react to that signal it doesn't know of? What's more, whether or not an observer wishes to be notified of a certain event may depend on its current state that an external object may not be aware of!

My opinion is that if there is a signal that an observer should react to, then it must 'know' of it it in the sense that it knows its interface (or an interface the observer can use to interact with it), and use that interface to register its wish to be notified.

Why must there be a separate object hat is responsible for registering the observer? And: is it even sensible to have such an object? In the system I mentioned in my previous post we've found that we had to strictly limit the notifications to the absolute minimum, just to be able to satisfy the hard real time conditions! We simply couldn't afford to wake up every observer every time a signal that might interest them popped up. Instead, we needed the observers to be able to switch off notifications during phases where they didn't matter - and only the observers themselves knew when that would be.

When I think about it, the way we implemented the observer pattern left it to each subsystem to decide for themselves whether it was the observer who did the registration or an external object. In fact, we could have one independent class for each association of a signal to an observer. However, we found that in all but a few cases it simply didn't make sense: most of the observers had to be able to switch notifications on and off at all times, just so they wouldn't get swamped by them. Anything short of that would have led to violations of hard real time conditions.

I don't argue the flexibility of signals and slots, but my experience shows that you trade a significant amount of performance for this flexibility. Depending on the system you implement, wou will have to decide which is your priority.

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.