Design question.... parent/child class

Peter Hansen peter at engcorp.com
Tue Sep 3 09:01:20 EDT 2002


Gumuz wrote:
> hi all,
> 
> i have a SessionManager class, which contains a list of Session
> objects(instantiated from a Session class). I have a design problem.
> 
> Each session object has a small 'queue' of messages for the specific
> session. How can a session send a message to another session without having
> some kind of parent-reference to the SessionManager object?
> 
> i am a bit puzzled, or is this parent-reference not such a bad idea after
> all?

Increasing coupling is always bad, but you could do this in a way that
doesn't really increase coupling (at least in Python).  The Session
objects don't need to know about SessionManagers specifically, just
about some thing that has a particular interface.  In this case, maybe
it's a "post" method.  Pass in a reference to the SessionManager,
or even the post method itself (a "bound" method, bound to a specific
SessionManager instance), but don't do an "import sessionmanager"
or anything like that in the session.py module itself.

Note that passing messages between Sessions could be considered a
function not appropriate for something called merely "SessionManager".
Maybe you want to do this with another mechanism in parallel to this,
or rename that class.  Personally, I wouldn't do that unless the
whole design was starting to get complex.

-Peter




More information about the Python-list mailing list