Builder Pattern

# Builder

- Purpose
- Structure
- Interaction
- Applications
- Consequences

## Purpose

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

## Structure

- Builder : specifies an abstract interface for creating parts of a Product object.
- ConcreteBuilder : Constructs and assembles parts of the product by implementing the Builder interface. Defines and keeps track of the representation it creates. Provides an interface for retrieving the product (e.g., GetASCIIText, GetTextWidget).
- Director : Constructs an object using the Builder interface.
- Product : Represents the complex object under construction. ConcreteBuilder builds the product's internal representation and defines the process by which it's assembled.

## Interaction

- The client creates the Director object and configures it with the desired Builder object.
- Director notifies the builder whenever a part of the product should be built.
- Builder handles requests from the director and adds parts to the product.
- The client retrieves the product from the builder.

## Applications

- The algorithm for creating a complex object should be independent of the parts that make up the object and how they're assembled.
- The construction process must allow different representations for the object that's constructed.

## Consequences

- It lets you vary a product's internal representation. The Builder object provides the director with an abstract interface for constructing the product. The interface lets the builder hide the representation and internal structure of the product. It also hides how the product gets assembled. Because the product is constructed through an abstract interface, all you have to do to change the product's internal representation is define a new kind of builder.
- t isolates code for construction and representation. I The Builder pattern improves modularity by encapsulating the way a complex object is constructed and represented. Clients needn't know anything about the classes that define the product's internal structure; such classes don't appear in Builder's interface.
- It gives you finer control over the construction process. Unlike creational patterns that construct products in one shot, the Builder pattern constructs the product step by step under the director's control. Only when the product is finished does the director retrieve it from the builder. Hence the Builder interface reflects the process of constructing the product more than other creational patterns. This gives you finer control over the construction process and consequently the internal structure of the resulting product.