GofPatterns Gofpatterns 





Creational Patterns  «Prev  Next»

Builder Pattern

The Builder pattern separates the construction of a complex object from its representation so the same construction process can create different objects. The Builder pattern allows a client object to construct a complex object by specifying only its type and content. The client is shielded from the details of the object's construction. This simplifies the creation of complex objects by defining a class that builds instances of another class. The Builder pattern produces one main product and there might be more than one class in the product, but there is always one main class. When you use the Builder pattern, you create the complex objects one step at a time. Other patterns build the object in a single step.
Builder pattern consisting of abstract Builder class and ConcreteBuilder
Builder pattern consisting of abstract Builder class and ConcreteBuilder

Builder Pattern in Java


Benefits of the Builder Pattern
The following lists the benefits of using the Builder pattern:
  1. Allows you to vary a product's internal representation.
  2. Isolates code for construction and representation.
  3. Gives you greater control over the construction process.
When To Use:
You should use the Builder pattern when:
  1. The algorithm for creating a complex object should be independent of both the parts that make up the objects and how these parts are assembled.
  2. The construction process must allow different representations of the constructed object.
The "Builder" pattern is an object creation software design pattern. The intention is to abstract the steps of construction so that different implementations of these steps can construct different representations of objects. Often, the builder pattern is used to build products in accordance with the composite pattern.
The intent of the Builder design pattern is to separate the construction of a complex object from its representation. By doing so, the same construction process can create different representations.
Builder
The builder is an abstract interface for creating objects.