Although the implementation of Adapter is usually straightforward, here are some issues to keep in mind:
The first step, which is common to all three of the implementations discussed here, is to find a ``narrow'' interface for Adaptee, that is, the smallest subset of operations that lets us do the adaptation. A narrow interface consisting of only a couple of operations is easier to adapt than an interface with dozens of operations. For TreeDisplay, the adaptee is any hierarchical structure. A minimalist interface might include two operations, one that defines how to present a node in the tree, and another that retrieves the node's children.
Given this narrow interface, here are three possible implementation approaches:
DirectoryTreeDisplay specializes the narrow interface so that its DirectoryBrowser client can use it to display directory structures.
For example, suppose there exists a DirectoryBrowser that uses a TreeDisplay as before. DirectoryBrowser might make a good delegate for adapting TreeDisplay to the hierarchical directory structure. In dynamically typed languages like Smalltalk or Objective C, this approach only requires an interface for registering the delegate with the adapter. Then TreeDisplay simply forwards the requests to the delegate. NEXTSTEP uses this approach heavily to reduce subclassing.
Statically typed languages like C++ require an explicit interface definition for the delegate. We can specify such an interface by putting the narrow interface that TreeDisplay requires into a purely abstract TreeAccessorDelegate class. Then we can mix this interface into the delegate of our choice---DirectoryBrowser in this case---using inheritance. We use single inheritance if the DirectoryBrowser has no existing parent class, multiple inheritance if it does. Mixing classes together like this is easier than introducing a new TreeDisplay subclass and implementing its operations individually.
For example, to create TreeDisplay on a directory hierarchy, we write
If you're building interface adaptation into a class, this approach offers a convenient alternative to subclassing.directoryDisplay := (TreeDisplay on: treeRoot) getChildrenBlock: [:node | node getSubdirectories] createGraphicNodeBlock: [:node | node createGraphicNode].