[Zope3-dev] Re: "most specific" interface?

Philipp von Weitershausen philipp at weitershausen.de
Wed Sep 14 05:18:42 EDT 2005


Jean-Marc Orliaguet wrote:
> Philipp von Weitershausen wrote:
> 
> 
>>Jean-Marc Orliaguet wrote:
>> 
>>
>>
>>>is the order of the list of interfaces implemented by an object subject
>>>to internal changes?
>>>
>>>I have identified the need for such a pattern:
>>>
>>>   iface = object.interface()
>>>
>>>with:
>>>
>>>class someObject(object):
>>>   implements(IMainInterface, ISecondaryInterface, ...)
>>>   def interface():
>>>       """Return the most specific interface implemented by the element."""
>>>       return list(providedBy(self))[0]
>>>
>>>to be able in that case to get access to the first interface implemented
>>>by an object, as a sort of main object type.
>>>   
>>>
>>
>>We usually do this differently. If some interfaces are special types
>>(e.g. IFile is a content type) then we have this interface provide
>>ISpecialType (e.g. IFile provides IContentType). ISpecialType is an
>>interface extending IInterface.
>>
>>Then, no matter where in the list of provided interfaces the type is, it
>>can be fetch with queryType. Let's take the IFile example from above and
>>set it up as a content type:
>>
>> >>> from zope.app.content.interfaces import IContentType
>> >>> from zope.app.file.interfaces import IFile
>> >>> from zope.interface import directlyProvides
>> >>> directlyProvides(IFile, IContentType)
>>...
>>
>>Philipp
>> 
>>
> 
> 
> I see, this is clever, and it simplifies the code.
> 
> the idea is that you define as many categories as you need: IMetaType,
> ISomeCategory, IWidgetType ... and you create relations between
> interfaces with:
> 
>     directlyProvides(IFile, IContentType)

Exactly.

> as if you had a relation tool, then every object that implements IFile
> (no matter in what position) will have the IFile content type?

Yes.

> But where do you put the 'directlyProvides' statement?

Like Jim said, anywhere in Python code. The fact that IFile directly
provides IContentType isn't bound to any class statement.

The typical case, though, is to use ZCML, as Jim also mentioned:

  <interface
      interface=".interfaces.IFile"
      type="zope.app.content.interfaces.IContentType"
      />

See the beginning of chapter 5 (page 55) of my book for a further
explanation and an example from the interpreter shell.

Philipp


Philipp


More information about the Zope3-dev mailing list