Newsgroups: comp.lang.smalltalk
Path: cantaloupe.srv.cs.cmu.edu!rochester!cornell!travelers.mail.cornell.edu!news.kei.com!news.mathworks.com!uunet!taligent  !taligent!kip-57.taligent.com!user
From: gamma@taligent.com (Erich Gamma)
Subject: Re: Using Blocks for Parameterized Adapters
Message-ID: <gamma-1803950041150001@kip-57.taligent.com>
Sender: usenet@taligent.com (More Bytes Than You Can Read)
Organization: Taligent, Inc.
References: <COMAS.95Mar15105322@bandol.csusa.ska.com>
Date: Sat, 18 Mar 1995 08:41:15 GMT
Lines: 52

In article <COMAS.95Mar15105322@bandol.csusa.ska.com>,
comas@bandol.csusa.ska.com (Andrew Comas) wrote:

> I don't understand Gamma, et al's using blocks for parameterized
> adapters in their book "Design Patterns"
> 
> In their chapter on Adapters p.145 they state:
> 
> ....
> 
> For example, to create TreeDisplay on a directory hierarchy, we write
> 
> directoryDisplay :=
>         (TreeDisplay on: treeRoot)
>                 getChildrenBlock:
>                         [:node | node getSubdirectories]
>                 createGraphicNodeBlock:
>                         [:node | node createGraphicNode].
> "
> 
> I don't understand how 'blocks' fulfill this requirement.  Could some
> explain this code in more detail or flush out this example.
>

For example, here is how TreeDisplay can be adapted to
show the class hierarchy starting at the class Object:

TreeDisplay on: (Object class)
             getChildrenBlock: 
                    [:class | class subclasses]
             createGraphicNodeBlock: 
                    [:class | "create a text label for: class name"].

TreeDisplay uses these two blocks in its BuildTree
method to traverse the tree and to convert a node into
a graphical presentation:

BuildTree:aNode

  | itsGraphic itsChildren |
   itsGraphic := createGraphicBlock value: aNode.
   itsChildren := getChildrenBlock value: aNode.
   ...
   itsChildren do: [:each | self BuildTree: each].
   ...

In other words the blocks passed to TreeDisplay
adapt the different interfaces (getSubdirectories:, subclasses:)
to access a tree structure to the common value: interface.

--erich gamma
gamma@taligent.com
