Observer Pattern (if you got some spare time)

Troy Melhase troy at gci.net
Tue Jun 17 02:02:03 EDT 2003


Tim Rowe wrote:

> I'm interested in the comment on class PubSub: "you should only need
> one instance of this class". It looks to me that it would be quite
> hard to manage that on a big project; if you made the one instance a
> global that makes class reuse harder. If it's local you have to make
> it available to anything that needs it. 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?  Gamma's solution does have memory
> management problems in C++ (I've still not worked out how to make it
> robust in the presence of observers being deleted), but I don't think
> those issues come up in Python.

A quick read of the code suggests that the comment is in error; I don't see
any reason why clients couldn't construct multiple instances of the PubSub
class.  There are no classmethods, no staticmethods, and no references to
the PubSub class by any code other than the example client at the bottom. 

There might be a use case that I'm not seeing, but I'd bet that Patrick
implied a behavior he didn't need with the comment.

-troy




More information about the Python-list mailing list