How to design that?

Hi everyone,

I have a small design issue.
I have a class A observing another B that is connected to an XMPP server. A waits for B signals. So far no prob!
However, I need to set a function member in another class (maybe in A) that launch the connexion between B and the server. Thus, the observer would pilot the observable. Could it be correct? It sounds like a non-sense, isn't it?

Thanks for your answers!
The problem is not absolutely clear to me, but are you looking for something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <functional>

struct B
{
    void do_it( std::function< void(int) > fn, int arg ) { fn(arg) ; }
};

struct A
{
    void recv_notification( B* b )
    { b->do_it( [this]( int a ) { this->some_task(a) ; }, 100 ) ; }

    void some_task( int a ) { std::cout << "do something arg==" << a << '\n' ; }
};

int main()
{
    A a ;
    B b ;
    a.recv_notification(&b) ;
}


Note: The code above requires a current compiler. The same idea (a call-back of a member function) could be implemented (differently) to work with older compilers.
Yes, obviously it was not clear enough.
In fact, i did'nt reallly want to design a pair observer/observable. I just wanted some advice about: how to trigger with elegance an action from the observer to the observable although ther's already a link in the other way (from the observable to the observer). Would it be feasible in the rule of C++ art? I'm pretty concerned of this 2 way signals design.

so you wanna code custom event system library?

well I'm currently coding my own for learning purposes and it works just fine, stil have some work to do...

here is short description of what I did:

created Event class, EventHandler class, Base class with virutal methods to hook and unHook from events plus some operators to register events with eventHandlers.
also Emit class to send signal trough application...
also a bunch of helper methods etc etc...

so, to tell you the truth there is no easy way to send and receive signals from one to anoter class.

basicaly you have 2 option:
1. code your own library
2. use exiting like boost::signals or any other.

if u wann boost here is link:
http://www.boost.org/doc/libs/1_48_0/doc/html/signals.html

hope this help :D
> In fact, i did'nt reallly want to design a pair observer/observable. I just wanted some advice about:
> how to trigger with elegance an action from the observer to the observable

Use the mediator pattern, perhaps? http://sourcemaking.com/design_patterns/mediator
Along with the command pattern? http://sourcemaking.com/design_patterns/command
Topic archived. No new replies allowed.