[Zope3-dev] Re: "most specific" interface?
Jean-Marc Orliaguet
jmo at ita.chalmers.se
Tue Sep 13 06:43:39 EDT 2005
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)
as if you had a relation tool, then every object that implements IFile
(no matter in what position) will have the IFile content type?
But where do you put the 'directlyProvides' statement? in the class :
class SomeClass:
implements(ISpecialFile, IFile)
directlyProvides(IFile, IContentType)
or in the code? as with:
class SomeClass:
def someMethod(self):
if this_is_a_file_after_all:
directlyProvides(IFile, IContentType)
does it apply to the class in which the code is located in that case? I
suppose that the directlyProvide statement executed last is the one that
matters?
/JM
More information about the Zope3-dev
mailing list