Joseph D. Darcy's Sun Weblog

Joseph D. Darcy's Sun Weblog


20070514 Monday May 14, 2007

Closures, Visitors, and Hedgehogs

It has been said that there are foxes and there are hedgehogs and "the fox knows many tricks, but the hedgehog knows one great trick." The one great trick of computer science is adding a level of indirection in the right place. There is an interesting similarity between the level of indirection added by the visitor pattern and by closures.

The visitor pattern when used on a type hierarchy allows arbitrary operations to be added independently by third parties after the hierarchy is made, at the upfront cost of an accept method and the visitor interface. Closures when supported in a programming language allow control APIs to be added independently of evolving the language, at the upfront cost of supporting closures in the language. A control API would be something similar to the enhanced for loop in JDK 5 operating on Collections implemented as a library method in java.util rather than a language change.

With both visitors and closures, the construct may be unfamiliar when first seen, but the utility of these patterns make them well worth learning. The extensibility afforded by visitors and closures creates much more scalable environments by allowing functionality to be added without the need for coordination from a single authority, either the type hierarchy or language maintainer.

(2007-05-14 08:03:35.0) Permalink Comments [6]

Calendar

« May 2007 »
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
16
17
18
19
20
21
22
23
24
25
26
27
28
30
31
  
       
Today

RSS Feeds

XML
All
/Annotation Processing
/General
/Java
/JavaOne
/Numerics
/OpenJDK

Search

Links

    Blogroll
  • Download the JRE

    News

Navigation



Referers

Today's Page Hits: 629