Wednesday, May 21, 2008

C++ Observer Pattern

Wednesday, May 21, 2008

What is Observer Pattern?

  • Observer pattern is a behavioral design pattern.
  • Observer pattern is used to solve the problem of notifying multiple objects of a change to keep them in sync like the Model-View-Controller (MVC) concept.
  • Useful for event management kind of scenarios.
  • Two classes are involved.
  • The Observable class is where the actual data change is occuring. It has information about all the interested objects that need to be notified.
  • The Observer is typically an abstract class providing the interface to which concrete classes interested in the events need to be compliant to.

Sample Program

#include <iostream>
#include <set>

using namespace std;

// ---------------- Observer interface -----------------
class MyObserver {
    public:
        virtual void Notify() = 0;
};

// ---------------- Observable object -------------------
class MyObservable {
        static MyObservable* instance;
        set<MyObserver*> observers;
        MyObservable() { };
    public:
       static MyObservable* GetInstance();
       void AddObserver(MyObserver& o);
       void RemoveObserver(MyObserver& o);
       void NotifyObservers();
       void Trigger();
};

MyObservable* MyObservable::instance = NULL;

MyObservable* MyObservable::GetInstance()
{
    if ( instance == NULL ) {
       instance = new MyObservable();
    }

    return instance;
}

void MyObservable::AddObserver(MyObserver& o)
{
    observers.insert(&o);
}

void MyObservable::RemoveObserver(MyObserver& o)
{
    observers.erase(&o);
}

void MyObservable::NotifyObservers()
{
    set<MyObserver*>::iterator itr;
    for ( itr = observers.begin();
          itr != observers.end(); itr++ )
    (*itr)->Notify();
}

// TEST METHOD TO TRIGGER
// IN THE REAL SCENARIO THIS IS NOT REQUIRED
void MyObservable::Trigger()
{
    NotifyObservers();
}

// ------ Concrete class interested in notifications ---
class MyClass : public MyObserver {

        MyObservable* observable;

    public:
       MyClass() {
          observable = MyObservable::GetInstance();
          observable->AddObserver(*this);
       }

       ~MyClass() {
          observable->RemoveObserver(*this);
       }

       void Notify() {
            cout << "Received a change event" << endl;
       }
};

void main()
{
    MyObservable* observable = MyObservable::GetInstance();
    MyClass* obj = new MyClass();
    observable->Trigger();
}

OUTPUT:
Received a change event

3 comments:

  1. I think your implementation lacks an important feature of the Observer pattern. An Observer object cannot know which of its Observable is notifying itself to the Observer through the Notify() method.

    To correct this, you can declare the Notify method as follow :
    virtual void Notify(Observable *iObservable) = 0;

    And in the NotifyObservers() method of the Observable, call Notify(this) instead of Notify().

    Philippe

    ReplyDelete
  2. Hi Could you please correct the code as Philippe mentioned if that is correct.

    Thanks

    Jee

    ReplyDelete
  3. This was well expained and the correction is required if you have more than one observable classes.

    Madan

    ReplyDelete

 

© 2013, 2014 Programming Tutorials by SourceTricks. All rights resevered. Designed by Templateism

Back To Top