Click here to Skip to main content
15,868,101 members
Articles / Programming Languages / C++/CLI
Article

Observer Pattern

Rate me:
Please Sign up or sign in to vote.
3.20/5 (10 votes)
9 Oct 20022 min read 73.6K   168   26   14
An article that explains Observer patterns in a simple manner

Introduction

Understanding the patterns and it's usage had been a cumbersome job when I started first. In addition, I could not get hold of any material where I could get them in the simplest form. So I thought I would just give a try for the guys who are just about to start on it. What I've suggested over here is one of the many possible way that we can implement this pattern. :-)

Details

As the name suggests, it is involved in the monitoring / observing the change of state in any object. To have a brief idea about the mechanism we have 3 components involved and work in tandem with each other.

  1. The class which is to be observed i.e. Observerable
  2. The Client class which is interested in knowing the state changes of this Observerable class
  3. The class which acts as the mediator between Client and Observerable.

Let's see the structure of this classes for what minimum things that they should have :-

class Observerable
{
public:
    bool SetObserver(Observer* apObserver ) 
    {
        if ( !apObserver )
            return false;

        myObserver = apObserver; 

        return true;
    }

    void StateChanged( ) 
    {
        cout << endl << "State Changed";
        myObserver->Notify();
    }
private:
    Observer* myObserver;
};

This is the class which is to be observed, we have a method SetObserver to set the mediator / observer class object in it. The Observer class is the mediator between the Client class and the Observable class, so the Observerable class maintains the object. So in case of any change, the Observer is notified and in turn the notification is delegated to the clients ( we will see the structure of the Observer class below).

class Client
{
public:
    void Update() { cout << "Update" << endl; }
};

The Client class will implement the method Update which is later called by the Observer class in order to notify the Client and the Client updates himself accordingly, so each Client can have its own implementation for the update and do different things with the same notification. ( pretty much same as document view architecture huh !! )

class Observer
{
public:
    bool Attach(Client* );
    bool Detach(Client* );
    void Notify( );

private:

    list<Client*> myClient;
};

This class allows the Client to attach or detach itself from getting the notification for the Observerable's changes; also this class maintains the list of clients, which it iterates and calls the update method on each client when notified by the Observerable class. I have not given the base class for any of the above but we can have a base class which defines some concrete methods. You can have the full source code from the zip I've attached.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralVery helpful Pin
Hans J. Schroeder30-Oct-02 8:33
Hans J. Schroeder30-Oct-02 8:33 
GeneralInteresting but very incomplete. Pin
Marc Clifton10-Oct-02 6:25
mvaMarc Clifton10-Oct-02 6:25 
GeneralRe: Interesting but very incomplete. Pin
Jörgen Sigvardsson10-Oct-02 12:04
Jörgen Sigvardsson10-Oct-02 12:04 
GeneralRe: Interesting but very incomplete. Pin
Marc Clifton10-Oct-02 14:59
mvaMarc Clifton10-Oct-02 14:59 
GeneralRe: Interesting but very incomplete. Pin
Nilesh Karkhanis10-Oct-02 19:55
Nilesh Karkhanis10-Oct-02 19:55 
GeneralRe: Interesting but very incomplete. Pin
Maximilien5-Nov-02 8:02
Maximilien5-Nov-02 8:02 
GeneralRe: Interesting but very incomplete. Pin
Jörgen Sigvardsson5-Nov-02 8:59
Jörgen Sigvardsson5-Nov-02 8:59 
GeneralRe: Interesting but very incomplete. Pin
Nilesh K.5-Nov-02 16:53
Nilesh K.5-Nov-02 16:53 
GeneralRe: Interesting but very incomplete. Pin
Maximilien6-Nov-02 5:45
Maximilien6-Nov-02 5:45 
GeneralRe: Interesting but very incomplete. Pin
Gary Kirkham10-Apr-03 10:53
Gary Kirkham10-Apr-03 10:53 
GeneralRe: Interesting but very incomplete. Pin
Jörgen Sigvardsson10-Apr-03 11:51
Jörgen Sigvardsson10-Apr-03 11:51 
GeneralRe: Interesting but very incomplete. Pin
Gary Kirkham10-Apr-03 12:20
Gary Kirkham10-Apr-03 12:20 
GeneralRe: Interesting but very incomplete. Pin
Jörgen Sigvardsson10-Apr-03 13:25
Jörgen Sigvardsson10-Apr-03 13:25 
GeneralRe: Interesting but very incomplete. Pin
Nilesh Karkhanis10-Oct-02 19:52
Nilesh Karkhanis10-Oct-02 19:52 

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.