LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 11-12-2003, 08:37 PM   #1
digitized_funk
Member
 
Registered: Sep 2002
Location: Australia
Distribution: Suse 8.0
Posts: 50

Rep: Reputation: 15
need some advice on c++ observer design pattern


g'day guys,

I need some architecture advice on the observer pattern and similar. I have a clock class. I want this clock class to emit two types of events, tick and halfminute events. There should be multiple subscribers to each event type. The subscribers should then be able to gather state information from the clock once notified of the change.

please describe the class structure I should use, I can't figure out how to apply observer pattern to emit multiple events from a single class?

cheers.
 
Old 11-13-2003, 11:39 AM   #2
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
What have you implemented already?

Are all the observers of the same type?

One basic approach for something a failry simple, would be have a container of pointers to all the observers. When your time event occurs you iterate through the container for all subscribed observers.

A basic example might look like this:

Code:
#include <iostream>
#include <vector>

using namespace std;

typedef int tick;         // I put these in here for simplicity
typedef int halfmin;    // you can use whatever class containes your time information

class Obs                // a basic observer with update functions
{
public:
  void tick_fun( const tick& t )  // update function with simple report
  {
    cout << "Tick " << t << '\n';
  }

  void halfmin_fun( const halfmin& hm ) // another update function
  {
    cout << "Halfmin " << hm << '\n';
  }
};

class Clock
{
public:
  vector<Obs*> tick_watchers;
  vector<Obs*> halfmin_watchers;
 
  void add_tickw( Obs& a )            // another observer subscribes to tick
  {
    tick_watchers.push_back( &a );
  }
  void add_halfminw( Obs& a )       // another observer subscribes to halfmin
  {
    halfmin_watchers.push_back( &a );
  }

  void on_tick( tick t )                    // a tick occurs - where the value t comes from
  {                                               // depends on your implentation
    for( vector<Obs*>::iterator i=tick_watchers.begin();
	 i!=tick_watchers.end();
	 ++i )
      {
	(*i)->tick_fun( t );
      }
  }

  void on_halfmin( halfmin hm )           // iterate through all the observers
  {
    for( vector<Obs*>::iterator i=halfmin_watchers.begin();
	 i!=halfmin_watchers.end();
	 ++i )
      {
	(*i)->halfmin_fun( hm );
      }
  }
  
};

int main()
{
  Obs ob[10];                     // Create some observers
  Clock c;                          // and a subject

  for( int i=0; i<7; ++i )
    c.add_tickw( ob[ i ] );        // subscribe some observers to ticks

  for( int i=4; i<10; ++i )
    c.add_halfminw( ob[ i ] );   // subscribe siome observers to halfmins
 
  c.on_tick( 100 );               // what triggers this is up to you
  c.on_halfmin( 4 );

}
Obviously, if you use virtual functions you can get different functionality. I would have a look at sigc++ ( http://libsigc.sourceforge.net/ ) which I think has more versatile implemantation using templates - I haven't really looked at it in detail.

I would add (disclaimer!) that I am aware of design patterns rather than familiar with them, so this is just my interpretation ... caveat emptor, as they say

Last edited by dakensta; 11-13-2003 at 11:41 AM.
 
  


Reply


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
Linux for Graphic Design, web design, and publishing maelstrom209 Linux - Software 8 07-17-2011 12:35 PM
printing pattern match and not whole line that matches pattern Avatar33 Programming 13 05-06-2009 07:17 AM
using grep when the pattern contains a ! farmerjoe Programming 9 03-16-2005 12:04 AM
page observer delos Linux - Software 2 01-25-2005 08:35 AM
WebSite Design Advice To All + Question petercool General 1 07-03-2002 11:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:36 AM.

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