On Mon, 16 Jul 2007 12:26:13 -0700, Tom <ku****@gmail.c omwrote:
This is probably a really basic question, but I know someone will be
able to help me. I have a single object A that contains a hash of
objects B. Each object B contains its own TCP client object containing
a connection to a server somewhere.
There is also a completely separate hash of objects C at the same
place as Object A. When I get TCP data into the TCP client I need to
let all of the object C's know about it. I Was wondering if I could
have all of the C objects register for the B events?
Is object A at all relevant? You didn't mention it with respect to the
communication between the objects B and objects C, so for the moment I'm
essentially (mostly) ignoring your first paragraph.
Anyway, yes...you can have each object C subscribe to an event on each
object B if you like. Every time an object C is instantiated, it would
have to subscribe to every object B's event, and every time an object B is
instantiated, each object C would need some way to know so that each can
subscribe to the new object B's event.
You may want to consider a "mediator"-based design, in which there's an
intermediate class that handles the connections between objects C and
objects B. Perhaps this is object A, or maybe you will want to make
another object to do this. You don't provide enough specifics for anyone
else to suggest which is better, so you'll have to decide that for
yourself.
The way the mediator would work is that objects C would all subscribe to a
single event on the mediator, while the mediator would subscribe to each
of the objects B event. Then when the mediator receives the event raised
by any object B, it would forward that event to its own event, which would
automatically result in each object C receiving the event.
This way each object C only needs to deal with a single object (the
mediator) and does not need to concern itself with the instantiation of
new objects B, while each object B would only have a single object (the
mediator) subscribed to its own event. Also, it encapsulates the
management of dealing with instantiations of each object B into a single
class (a nice way to do that would be to make the object B class aware of
the mediator, and register itself when it's instantiated, providing a
method in the mediator that in turn subscribes the mediator to the object
B event).
Pete