observer pattern - message handling/passing interface
- From: ma740988@xxxxxxxxx
- Date: Tue, 19 Aug 2008 05:21:34 -0700 (PDT)
My system consists of a platform, a controller and pumps (four max).
The controller serves as a - if you will - a bridge and communicates
with the controller and pumps.
I have a litany of messages used during for communicating with
platform->controller and controller->pumps OR pump->controller and
controller->platform.
Message between platform->controller and controller->pump are similar
but needs to be remapped - if you will. Example scenario:
a) Controller receives a incoming message from platform.
b) Controller determines the appropriate type (1a,2a,etc) and updates
the platform message.
c) The pump(s) (could be 1,2,3 or 4...I'm thinking observer design)
get notified accordingly. The pump(s) then copy the contents from the
platform message type into their own type.
So I have a message class for platform to controller and a class for
controller to pump. So now:
////////////////////////////////////
message.h ////////////////////////////////////
# ifndef MESSAGE_H
# define MESSAGE_H
/*
-----------------------------------------
| message class platform to controller
-----------------------------------------
*/
// message class
class pform_ctrller_msg1a {
public :
pform_ctrller_msg1a () {}
~pform_ctrller_msg1a() {}
void SetData() {}
void GetData() {}
};
// similarily
class pform_ctrller_msg2a {
public :
pform_ctrller_msg2a () {}
~pform_ctrller_msg2a() {}
void SetData() {}
void GetData() {}
};
// etc up to pform_ctrller_msg22a
/*
-----------------------------------------
| message class controller to pump
-----------------------------------------
*/
class ctrller_to_pump_1a {
public :
ctrller_to_pump_1a () {}
~ctrller_to_pump_1a () {}
};
// similarily
class ctrller_to_pump_2a {
public :
ctrller_to_pump_2a () {}
~ctrller_to_pump_2a () {}
};
// etc up to ctrller_to_pump_22a
# endif
I'm thinking this is a candidate for an observer design. Thus far
I've got:
////////////////////////////////////
observer.h ////////////////////////////////////
# ifndef OBSERVER_H
# define OBSERVER_H
#include <list>
#include <iostream>
#include <vector>
using namespace std ;
class subject;
class observer {
public:
observer() {};
~observer() {};
virtual void Update(subject* theChangeSubject) = 0;
};
class subject {
public:
subject() {};
~subject() {};
void Attach( observer* o ) { _observers.push_back( o ); }
void Detach( observer* o ) {
int const count = _observers.size();
int idx = 0 ;
for (; idx < count; ++idx ) {
if(_observers[ idx ] == o) {
break;
}
if( idx < count ) {
_observers.erase( _observers.begin() + idx );
}
}
}
void Notify() {
int const count = _observers.size();
for ( int idx = 0; idx < count; ++idx )
( _observers[ idx ] )->Update( this );
}
private:
std::vector< observer* > _observers;
};
# include "message.h"
/*
| controller interface
| upon receipt of incoming data this class will
| update pform_ctrller_msgXa where:
| X reflects a 1a, or 2a message or ..
*/
class ctroller_if : public subject {
pform_ctrller_msg1a pc_mgs1a ;
//pform_ctrller_msg2a pc_mgs2a ;
//etc up to 22a
public :
ctroller_if () {}
~ ctroller_if () {}
void store_hdr ( unsigned char* hdr ) {
/*
| All incoming messages gets parsed here...
| The header has an ID that when parsed denotes if msg is a
| 1a, 2a, etc. etc.
| If header is a 1a msg, then we'll update pc_msg1a
| If header is a 2a msg, we'll update pc_msg2a
| and so on....
*/
}
unsigned short get_hdr () { return ( 1 ) ; }
pform_ctrller_msg1a& get_ctroller () { return pc_mgs1a ; }
};
class pump_a_observer : public observer {
ctroller_if* ptr_if; ;
public :
pump_a_observer ( ctroller_if *ptr_if_ )
: ptr_if ( ptr_if_ )
{ ptr_if ->Attach ( this ); }
void Update(subject* theChangeSubject) {
//ptr_if->get_hdr() ;
pform_ctrller_msg1a cmsg = ptr_if ->get_ctroller();
std::cout << " o " << std::endl;
}
};
#endif
////////////////////////////////////
main.cpp ////////////////////////////////////
# include "message.h"
# include "observer.h"
int main() {
std::cout << " [ ";
ctroller_if cif_obj ;
pump_a_observer pobsrv ( &cif_obj ) ;
cif_obj.Notify ( );
std::cin.get();
}
The question(s)
a) I'd like to know if I'm on the write track in terms of my choice of
design pattern - in part because the code right now is structured
along the lines of platform->controller->pump. I'm struggling with
the reverse direction pump->controller->platform. In terms of the
reverse direction the observer pattern doesn't appear to make sense.
b) If the choice of design pattern is solid, I'd like tips (sample
source would be great) on how I could create a generalized observer ..
perhaps an observer wrapper .. in other words templated source that'll
work in both directions.
c) The pump class could be one of four types. pump_a, pump_b, pump_c
or pump_d. Each pump has a set of uniqueness in terms of what they do
with a message. That aside, I'd like to believe that I could also
generalize the pump and class ctroller_if classes.
Thanks in advance.
.
- Prev by Date: Re: Encapsulation theory
- Next by Date: Why OOP Fails Domain Modeling
- Previous by thread: EliteOpenSource - Start-up
- Next by thread: Why OOP Fails Domain Modeling
- Index(es):
Relevant Pages
|
|