Saturday, November 24, 2007

Adapter pattern vs ZCA adapter

This is a section I added to ZCA book recently. I am not a pattern guru,
so please correct me if anthing is not correct or required more explanation.
Thanks !



Adapter pattern
~~~~~~~~~~~~~~~

The adapter concept in Zope Component Architecture and the classic
`adapter pattern` as described in Design Patterns book are very
similar. But the intent of ZCA adapter usage is more wider than the
`adapter pattern` itself. The intent of `adapter pattern` is to
convert the interface of a class into another interface clients
expect. This allows classes work together that couldn't otherwise
because of incompatible interfaces. But in the `motivation` section
of Design Patterns book, GoF says: "Often the adapter is responsible
for functionality the adapted class doesn't provide". ZCA adapter has
more focus on adding functionalities than creating a new interface for
an adapted object (adaptee). ZCA adapter lets adapter classes extend
functionality by adding methods. (It would be interesting to note
that `Adapter` was known as `Feature` in earlier stage of ZCA
design. ) [#feature]_

The above paragraph has a quote from Gang of Four book, it ends like
this: " ...adapted class doesn't provide". But in the next sentence I
used "adapted object" instead of "adapted class", because GoF
describes about two variants of adapters based on implementations.
The first one is called `class adapter` and the other one is called
`object adapter`. A class adapter uses multiple inheritance to adapt
one interface to another, on the other hand an object adapter relies
on object composition. ZCA adapter is following object adapter
pattern, which use delegation as a mechanism for composition. GoF's
second principle of object-oriented design goes like this: "Favor
object composition over class inheritance". For more details about
this subject please read Design Patterns book.

The major attraction of ZCA adapter are the explicit interface for
components and the component registry. ZCA adapter components are
registered in component registry and looked up by client objects using
interface and name when required.

.. [#feature] Thread discussing renaming of `Feature` to `Adapter`:
http://mail.zope.org/pipermail/zope3-dev/2001-December/000008.html