Latest LQ Deal: Latest LQ Deals
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-02-2012, 03:13 AM   #1
Senior Member
Registered: Dec 2008
Posts: 4,732
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Implementing Observer pattern when observers wish to observe different items

Below I have attempted to write a sudo code for the Observer pattern when observers wish to observe different items.
Ignore the syntax errors. I wish to know if this is the correct way to implement this. If not, please suggest better ways.

// Used by the subject for keeping a track of what items the observer wants to observe
typedef struct observerListStruct
    bool getTemperatureUpdate;
    bool getHumidityUpdate;
    bool getPressureUpdate;
    observer's-function pointer's address;

// Subject's class
class weatherData
        // Observers will call this function to register themselves. The function pointer will point to the function which will get called when updates are available.
        void registerObservers (observer obj, observer's-FunctionPointer)
            // This observer's function returns which items to observe.
            char* f = obj.returnItemsToObserve ();
            if f[0] = `1`
                observerListStruct.getTemperatureUpdate = true;

        void unregisterObservers (observer obj) {}

        vector <observerListStruct> observerList;
        float temperature;
        float humidity;
        float pressure;

        void notifyObservers (){}

        float getTemperature (){}
        float getHumidity ()   {}
        float getPressure ()   {}
} weatherDataObject;

// Base class for observers containing common functions
class observers
    char ItemsToObserve [3] = {1, 2, 3};

    // This observer's function returns which items to observe. Default - return all items
    virtual char* returnItemsToObserve ()
        return ItemsToObserve;

class observerDisplayElementCurrentConditions : public observers
    char ItemsToObserve [3] = {1, 2};

    char* returnItemsToObserve ()
        return ItemsToObserve;

    // this function will be used as a function pointer for getting updates
    void getUpdatesAndDisplayWeatherData (float, float) {}
Another way can be to implement a global table which'll contain the following:
Observer | Item to be observed | Function pointer addresss
Observer will fill up this table, and subject will read from the table and act accordingly?

Which one is the better way out, and why?

Last edited by Aquarius_Girl; 05-02-2012 at 03:45 AM.
Old 05-02-2012, 09:11 AM   #2
LQ Guru
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,275

Rep: Reputation: 5342Reputation: 5342Reputation: 5342Reputation: 5342Reputation: 5342Reputation: 5342Reputation: 5342Reputation: 5342Reputation: 5342Reputation: 5342Reputation: 5342
class WeatherData
    void registerTemperatureObserver(Observer *);
    void registerHumidityObserver(Observer *);
    vector<shared_ptr<Observer *>> temperatureObservers;
    void notifyTemperatureObservers();
    vector<shared_ptr<Oberver *>> humidityObservers;
    void notifyHumidityObservers();

class Observer
    virtual void notify(float) = 0;

Last edited by dugan; 05-02-2012 at 09:17 AM.
Old 05-02-2012, 09:27 AM   #3
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,715
Blog Entries: 4

Rep: Reputation: 3951Reputation: 3951Reputation: 3951Reputation: 3951Reputation: 3951Reputation: 3951Reputation: 3951Reputation: 3951Reputation: 3951Reputation: 3951Reputation: 3951
Definitely a better approach. If a client wants to observe more than one thing, let it register itself separately for each thing it wants to observe.

There are lots of ways to design such a thing, and I try to keep it as simple as possible. I do tend to design such systems such that, if a client wanted, say, to "observe temperature," the client is not only obliged to register itself as a temperature-observer, but also to define a particular method within itself, which anyone who wants to observe temperatures must implement appropriately. This is a simple but effective way to detect accidentally associating an observer-subscription with the wrong kind of handling routine. (It also simplifies the semantics of getting data to it, since both parties understand the abstract interface that each of them will use.) An object that wants to observe more than one thing would implement all of the required interfaces and then register itself, one by one, for everything it wants to observe.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] /bin/bash if statement pattern search, end of pattern special character? headhunter_unit23 Programming 3 04-29-2010 08:05 AM
[SOLVED] redirecting output, observe with tail -f <file>, while running, without screen? srons Linux - Newbie 3 04-22-2010 06:43 AM
new observer emarald Member Intro 1 05-03-2006 03:09 PM
netlink routing socket to observe interface changes trotters78 Programming 2 11-29-2004 06:13 PM
need some advice on c++ observer design pattern digitized_funk Programming 1 11-13-2003 10:39 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:12 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration