Click here to Skip to main content
Click here to Skip to main content
Go to top

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

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

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

Share

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. PinmemberJörgen Sigvardsson10-Apr-03 11:51 
GeneralRe: Interesting but very incomplete. PinmemberGary Kirkham10-Apr-03 12:20 
GeneralRe: Interesting but very incomplete. PinmemberJörgen Sigvardsson10-Apr-03 13:25 
GeneralRe: Interesting but very incomplete. PinmemberNilesh Karkhanis10-Oct-02 19:52 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 10 Oct 2002
Article Copyright 2002 by Nilesh K.
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid