Adaptive Builder Design Pattern

Name

Adaptive Builder

Author

Robert Familiar
bofam@kafka.iii.net

Example

Applications need a mechanism for building complex objects that is independent of the parts that make up the object.

Context

To build on the Builder Design Pattern as described in Design Patterns by GOF.

Problem

Decouple the process of building a complex object from the parts that make up the object.

Solution

Demeter Class Dictionaries, and the ability of the Demeter Tools to parse these object descriptions, solve the builder problem. (See the Demeter book for more information about Demeter.) For example, consider the following class dictionary for describing various types of bicycles:

	Bicycle = <color> Color ";" <parts_list> List( Part ) .
	PartList ~ { Part "," } .
	Part : Piece1 | Piece2 | Piece3 | Piece4 | Piece5 | Piece6 . 
	Piece1 = "tire" 
	Piece2 = "handle-bars" 
	Piece3 = "single-frame" 
	Piece4 = "double-frame" 
	Piece5 = "horn" 
	Piece6 = "seat" 
	Color : GreenColor | BlueColor .
	GreenColor = "green" .
	BlueColor = "blue" .
Any combination of these part names can now be input to this class dictionary to create a Bicycle Object. The Demeter Suite of Adaptive Software Tools can take a class dictionary definition like the one above and generate both the C++ code needed to instantiate all the class definitions as well as a parser that can read legal input to the class dictionary and build the resulting complex object. For example, the input sentence:

    blue; seat, frame, horn, tire, tire, handle-bars
can be used to describe a blue single seat bike. A green 2 seater bike can also be created:

    green; seat, seat, frame, horn, horn, tire, tire, tire, handle-bars, handle-bars
The class dictionary is independant of the mechanism that creates the object. The class dictionary can change over time without affecting the process by which Bicycle objects are created.

This method meets both the requirements of the Builder Design Pattern:

  1. the algorithm for creating a complex object must be independent of the parts that make up the object and how they are assembled
  2. the construction process must allow different representations for the object that is constructed
Using this method, there is no need to define the Director, Builder and ConcreteBuilder classes as described in GOF. The Builder Pattern is built in to the Demeter Tools.

When is it still useful to use the Builder pattern described in GOF?