a single class supporting multiple facets/interfaces

Jp Calderone exarkun at intarweb.us
Wed Jan 22 12:50:57 EST 2003


On Wed, Jan 22, 2003 at 12:51:02AM -0700, David Garamond wrote:
> > [snip code]
> 
> thanks. yes, actually "interface" and "component" do pretty much what i 
> envision. the components module from Twisted looks like it might be 
> something cool.
> 
> to correct myself and the previous example, the original requirement is 
> for a single class (component) C to implement two facets (interfaces). 
> let's call the two interfaces IGoodStuff1 and IGoodStuff2.
> 
> class IGoodStuff1(components.Interface):
>     def foo(self): pass
>     def bar1(self): pass
> class IGoodStuff2(components.Interface):
>     def foo(self): pass
>     def bar2(self, arg): pass
> 

  I don't think you can implement both of these interfaces for the same
Component without using Adapters.  As you say below, the problem is the
'foo' name conflict.  The best solution (other than creating two Adapters) I
can think of is to rename one of the foo methods.

  You could probably do a metaclass trick to create the Adapters
automatically from the Interface definitions, but that might be just a
little too much magic for your (or anyone's) own good.

> and since i don't think i want more than one Adapters for a single 
> component-interface link -- in other words: a component never implements 
> a single interface twice, differently -- can i do away without adapters 
> in this case? i.e.: i want to move the "adapter stuff" (like the 
> '__implements__' declaration) directly in C. something like this:
> 
> class C(components.Componentized):
>     __implements__ = (IGoodStuff1, IGoodStuff2)
>     def foo1(self):
>         # will implement IGoodStuff1's foo
>     def foo2(self):
>         # will implement IGoodStuff2's foo
>     def bar1(self):
>         # will implement IGoodStuff1's bar1
>     def bar2(self):
>         # will implement IGoodStuff1's bar2
> 
> the problem is of course the 'foo' name conflict.
> 
> -- 
> dave
> 

  I'm wondering if you want to do this for some kind of versioning scheme,
where objects might be upgraded over time (and so present different
implementations or interfaces)?  If so, another part of Twisted might be
useful, twisted.python.styles.Versioned.


  Jp

-- 
 12:00am up 37 days, 9:48, 2 users, load average: 0.36, 0.32, 0.27
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20030122/ffdb2035/attachment.sig>


More information about the Python-list mailing list