Builder Pattern

Role

The Builder pattern separates the specification of a complex object from its actual construction. 

 

The same construction process can create different representations.

Intent

  • Defines an instance for creating an object but letting subclasses decide which class to instantiate
  • Refers to the newly created object through a common interface

Design

Main Players

  • IBuilder
    • An interface defining what can be built
  • Director
    • A sequence of operations that can be followed to create a Product
  • Builder
    • A class that is called by a Director to make Product parts
  • Product
    • The object under construction, by parts

Example Class Diagram

Advantage of the Builder/Director partnership

Products do not all necessarily turn out the same.

 

TheBuilder can redefine the way it works and, even with the sameDirector, produce a different product.

 

Furthermore, the Builder pattern constructs the product step by step under the director’s control.

Use the Builder pattern when…

  • The object to be assembled might have different representations.
  • You need fine control over the construction process.

Comparison with

Abstract Factory

Resources

thanks....

@brs_snmz

Made with Slides.com