Builder Pattern


  • The Builder pattern separates the specification of a complex object from its actual construction.
  • The Builder pattern is based on Directors and Builders. Any number of Builder classes can be called by a director to produce a product according to specification.
  • A specific advantage of the Builder/Director partnership is that the products do not all necessarily turn out the same. The Builder can redefine the way it works and, even with the same Director, produce a different product.
  • Only when the construction is complete is the product returned to the director for the director to work with.

Class diagram: (see Builder Pattern code review)

Builder Pattern class diagram of sample code download


Use the Builder pattern when:

  • The algorithm for creating parts is independent from the parts themselves.
  • The object to be assembled might have different representations.
  • You need fine control over the construction process.