multiple extensions to a class
oliver.schoenborn at utoronto.ca
Mon Jul 12 05:34:29 CEST 2004
From: "David Fraser" <davidf at sjsoft.com>
> Jeff Shannon wrote:
> > Humpty Dumpty wrote:
> >> One possibility would be to define the extensions B,C and D as
> >> add those functions to A with new.instancemethod. Any other ideas on
> >> do this? E.g. is it possible to *add* base classes to a class? I could
> >> the extensions by adding them as base classes to A.
> > You might consider making class A into a container of some sort -- i.e.,
> > give it someplace to store a list of "decorator" objects, and then make
> > B, C, and D into such decorators (rather than subclasses of A). You'll
> > have to arrange some way of dispatching messages to A that are intended
> > for the decorators, and possibly some way for the decorators to talk to
> > each other.
Interesting idea, could be done... how does this differ from the mixin
> > (It's hard to say whether you'd be better off making B and
> > C separate decorators, or whether to subclass C from B -- depends on
> > your specific application.)
I could probably create a forwarding mechanism so that if a class doesn't
understand something, it passes on the request to each decorator in a list.
Then C could just be a decorator of B, just like B is of A.
> The advantage of Python here is that you don't need to subclass one from
> the other - just make a clear definition of the methods you expect, and
> then have a system where you call methods on a list of listening
Yes, but you have to be careful with that in terms of cohesion: though you
want to allow for extendability through plug-ins, you don't want to
encourage a mish mash of unrelated classes that send messages around. It's a
technique to keep in mind, but I want to maintain aggregation of logic and
function and data as much as possible, ie aim for "the right balance" :)
More information about the Python-list