AMDG Asif Lodhi wrote:
Hi Steven,
On 3/30/08, Steven Watanabe
wrote: ......................................................... There needs to be a way to register callbacks for each particular event type. Then, there is some code that gets a packet and decodes it to the proper type. Finally, this is dispatched to all the registered signals.
That's exactly why I suggested using the Observer design pattern.
How is anything suggested in this thread not an instance of
Observer, I might ask?
I guess the problem is: where do the callbacks get registered?
You can't register them with the event because the event doesn't
exist yet. You can create a global object to register for each Packet
type, but globals are not usually a good idea. It is also possible to
have a separate object for each type of Packet, but they are all
tightly bound together and all need to be available when the
Packet comes in. This leads to having one class that holds
all the callbacks. Now, we could create a separate signal
for each Packet type in the class body:
class SignalHolder {
public:
void operator()(const Packet1& packet) const { signal1(packet); }
void operator()(const Packet2& packet) const { signal2(packet); }
void operator()(const Packet3& packet) const { signal3(packet); }
...
void operator()(const PacketN& packet) { signalN(packet); }
private:
signal