[Zope3-dev] Re: Interface and Adapter Demo

Stephan Richter stephan.richter at tufts.edu
Wed Sep 1 10:26:56 EDT 2004


Hi Christian,

I finally got around to integrate your suggestions in the document and I
checked it in as part of the interface package. It's called human.txt. ;-)

On Sunday 27 June 2004 14:48, Christian Robottom Reis wrote:
> > For statistical reasons we often want to know the size of a file.
> > However, it would be clumsy to implement the size directly in the file
> > object, since the size really represents meta-data. Thus we create
> > another interface that provides the size of something.
>
> Just an orthogonal thought: is it really *that* clumsy to implement the
> size as part of the file object? Or it is more of a design decision that
> for certain situations makes sense and for others not?

From a component architecture point of view it is, since the file only
represents its content, nothing else. All information around that is
meta-data and should be handled by other objects.

> > Next we implement an adapter that can provide the 'ISize' interface given
> > any object providing 'IFile'. By convention we use '__used_for__' to
> > specify the interface that we expect the adapted object to provide, in
> > our case 'IFile'. However, this attribute is not used for anything.
>
> Is the convention to use a tuple if used for more than one Interface, or
> would that be a `bad thing'?

No a tuple is good in this case.

> > to extract the size from. Also by convention the context is stored in an
> > attribute 'context' on the adapter. The twisted community refers to the
>
> attribute named 'context' perhaps.
>
> Does it make sense to have adapter-specific names, such as naming the
> attribute `file' in this case, given the improved understandability of
> the adapter's code, or is the adapter's code usual too short for it to
> matter?

You could do that. But it is obvious, since you specify the interface of the
object that is being passed in. Using different names as arguments is
helpful, when dealing with multi-adapters, which are adapters that require
multiple input interfaces. I have purposefully left this part out of this
short introduction.

> > have only one original interface.
>
> I find this section a bit heavy on required concepts. You might want
> to use something from this alternative summary:
>
>     The registry keeps a map of what adapters implement an interface on
>     behalf of other interfaces. To register FileSize, an adapter that
>     adapts IFile-providing instances to the ISize interface, we use the
>
>     register() method::
>         >>> registry.register([IFile], ISize, '', FileSize)
>
>     The first argument is a list of original interfaces; in our case,
>     FileSize only adapts instances providing one interface, IFile.
>
>     Using a list makes sense when the same adapter is available for
>     multiple interfaces -- in our case, it might make sense to adapt
>     other `sizeable' objects, such as IString and even IDistance.

This is wrong! You specify multiple interfaces, when you deal with
multi-adapters. If you want to register the same adapter for several input
interfaces, you have to register it multiple times. However, this rarely
happens.

> > The third argument in the name of the adapter. Since we do not care
> > about names, we simply leave it as an empty string.
>
> Conventionally (perhaps Zope-conventionally <wink>), what are these
> names useful for?

Their uses are rare and I can't remember a good example. :-)

> Note that adapter.txt makes my brain feel soft (and someone else has
> said the same thing to me -- though perhaps just to make me feel
> better). It seems to be at points trying (and succeeding <wink>) *on
> purpose* to confuse me, via the use of integers (what is 12?) in place
> of "context".  This is actually compounded by the adapt_0_to_42 adapter
> hook exemplified in interface.py -- I was almost sure adapter_hooks were
> what I wanted but the example just didn't make sense -- it doesn't even
> use the iface argument, and nowhere does it suggest I might want to use
> the registry.

No, I think you are totally correct. When Jim starts talking about adapter IX
and IY or object 1 and 2 then I always loose him too.

> It would help tons just to add the simplest adapter hook that used the
> registry as an example, and point to it -- many people will need
> something more specific, but there's at least something to start from.

Well, I hope now that I put the file in the interface package, people have
something useful to work with.

Regards,
Stephan
--
Stephan Richter
CBU Physics & Chemistry (B.S.) / Tufts Physics (Ph.D. student)
Web2k - Web Software Design, Development and Training


More information about the Zope3-dev mailing list