Click here to Skip to main content
13,457,438 members
Click here to Skip to main content
Add your own
alternative version


27 bookmarked
Posted 9 Oct 2002

Observer Pattern

, 9 Oct 2002
Rate this:
Please Sign up or sign in to vote.
An article that explains Observer patterns in a simple manner


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. :-)


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
    bool SetObserver(Observer* apObserver ) 
        if ( !apObserver )
            return false;

        myObserver = apObserver; 

        return true;

    void StateChanged( ) 
        cout << endl << "State Changed";
    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
    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
    bool Attach(Client* );
    bool Detach(Client* );
    void Notify( );


    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.


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


About the Author

Nilesh K.
Web Developer
India India
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralRe: Interesting but very incomplete. Pin
Jörgen Sigvardsson10-Apr-03 11:51
memberJörgen Sigvardsson10-Apr-03 11:51 
GeneralRe: Interesting but very incomplete. Pin
Gary Kirkham10-Apr-03 12:20
memberGary Kirkham10-Apr-03 12:20 
GeneralRe: Interesting but very incomplete. Pin
Jörgen Sigvardsson10-Apr-03 13:25
memberJörgen Sigvardsson10-Apr-03 13:25 
GeneralRe: Interesting but very incomplete. Pin
Nilesh Karkhanis10-Oct-02 19:52
memberNilesh 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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180322.1 | Last Updated 10 Oct 2002
Article Copyright 2002 by Nilesh K.
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid