Builder

Intent

Separate the construction of a complex object from its representation so that the same construction process can create different representations.

Structure

Notes

The Builder design pattern is a Dynamic Behavior pattern in which the dynamized behavior is object construction. To see this consider the following correspondence between participants.

Builder pattern Dynamic Behavior pattern
Director Delegator
Builder Delegatee
ConcreteBuilder ConcreteDelegator

The Builder participant is often based on a parser. There are two important alternatives for using a parser to create different representations.

For large documents the Builder approach uses fewer resources since it does not have to build a large internal structure. The Composite/Visitor approach makes it easier to modify a document.

Examples

The difference between the Builder and Composite/Visitor approaches is best illustrated by considering the two main approaches for dealing with XML documents in the context of creating different representations.

See Events vs. Trees for a (somewhat biased) discussion of tradeoffs between the two approaches.