Observer Pattern (if you got some spare time)

Patrick Useldinger a at b.c
Tue Jun 17 12:40:19 EDT 2003


Tim Rowe wrote:

> I'm interested in the comment on class PubSub: "you should only need
> one instance of this class". 

You can, as Troy points out, have as many instances of the PubSub class 
as you wish; I just can't see *why* you would like to do that.

> The approach in Gamma et al
> looks to be to be far easier to manage: make each subject handle its
> own message queue rather than have a centralised despatcher.  Is there
> a reason you didn't go that route, given that Python supports the
> necessary multiple inheritance?  

Yes, serveral:

-1-
I prefer to avoid multiple inheritance, because this potentially raises 
a few problems and puts constraints on the classes that inherit, and 
therefore limits reusability.

-2-
Imagine you have a general-purpose class Tool with methods m1, m2, m3, 
which are supposed to be broadly reusable. In this solution, an instance 
of Tool will just attach itself to subjects "Tool.m1", "Tool.m2" and 
"Tool.m3". Any class who wants to use these methods will just publish 
those "well-known" subjects, and no objects ends up knowing that the 
other object exists. They all just know about the central dispatcher.

This approach decouples classes even more, and also allows for more 
reusability.

-3-
I wanted to have "multi-messages", a kind of broadcasting medium where 
everyone is allowed to publish information on the same subject. I cannot 
see how to do this easily with the standard pattern. With a central 
instance, you just agree on a common subject, that's all.


-Patrick
-- 
Real e-mail address is 'cHVAdm8ubHU=\n'.decode('base64')
Visit my Homepage at http://www.homepages.lu/pu/





More information about the Python-list mailing list