next up previous
Next: 1.5.3 Defining Instance Methods Up: 1.5.2 The Composite Pattern Previous: 1.5.2 The Composite Pattern

1.5.2.0.1 When Unions are Composite

The use of the union pattern in the DeptDirectory example has an extra feature not present in the preceding CityEntry example. One of the variants of the union class DeptDirectory includes a field of type DeptDirectory which makes the structure of the union class self-referential. Since self-referential structures are ubiquitous in Scheme (and other functional languages), this feature is not at all surprising to programmers familiar with functional programming. In the OOP community, which has strong historical ties to imperative programming, this feature is viewed as distinctive because it implies that methods that process the union class are naturally recursive. For this reason, the OOP community assigns a distinct pattern name, namely compositive pattern, to union patterns that include variants with self-references. We will use this terminology in the remainder of the monograph.

The following expression creates a DeptDirectory containing the address and phone information for Corky and Matthias:

new Cons(new Entry("Corky","DH3104","x 6042"), 
  new Cons(new Entry("Matthias","DH3106","x 5732"), new Empty()))
This syntax is wordy but straightforward. Don't forget to include the keyword new at the front on each constructor invocation!


next up previous
Next: 1.5.3 Defining Instance Methods Up: 1.5.2 The Composite Pattern Previous: 1.5.2 The Composite Pattern
Corky Cartwright
2000-01-07