Over a million developers have joined DZone.
Platinum Partner

Intro to Design Patterns: Builder Pattern

· Java Zone

The Java Zone is brought to you in partnership with ZeroTurnaround. Discover how you can skip the build and redeploy process by using JRebel by ZeroTurnaround.

Either because you're new to them, or as a refresher, here is the start of a series on the "Gang of Four" design patterns. Andre Mare, the author of the Java Design Concepts blog, is a J2EE Specialist and has more than seven years experience in development and design of enterprise systems. Here he starts a series of articles that aim to introduce you to the "Gang of Four" design patterns. -- Geertjan Wielenga, JavaLobby Zone Leader


Intent of the Pattern

The Builder Pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. - Gof


Object Creational


The Builder Pattern simplifies the construction of complex objects by only specifying the type and content that the object requires. The construction process of the complex object will therefore allow for different representations of the complex object to be created by the different builders. Each of the concrete builder objects will construct a different representation of the complex object.

The Builder Pattern consists of a Builder, ConcreteBuilder, Director and Product.

  • The Director object is responsible for the construction process of the complex object but delegates the actual creation and assembly to the Builder interface.
  • The Builder object specifies the interface for creating parts of the complex object.
  • The Product represents the complex object that is created by the ConcreteBuilder objects. The Product consists of multiple parts that are created separately by the ConcreteBuilder objects.
  • The ConcreteBuilder objects create and assemble the parts that make up the Product through the Builder interface.

A client object creates an instance of the Director object and passes it the appropriate Builder object. The Director object invokes the methods on the Builder object to create and initialize specific parts of the Product object. The Builder receives content from the Director object and adds these to the Product object. The Client object has a reference to the Builder object and retrieves the created Product object from it.


Design Pattern, Builder Pattern, GOF

The construction of the Complex object (Product) is hidden by the Builder objects from the Client and Director objects. To change the internal representation of the complex object, a new concrete builder object is defined and used by the client through the Director object. Unlike other creational patterns, the Builder Pattern creates the complex object is sections through the Director and Builder objects. The Builder object may need access to information that was used in previous construction steps. This means that even thought the parts of a Product object is created in individual sections; they may interact with other sections to create the complex product object.

The complex Product objects do not usually have a shared abstract parent object, as their representation differ and a shared parent class or interface might not be possible. As the client object specify the Builder object, it should have the knowledge how to handle the product object that is created by the Builder object.

Java Sample Code

The example for the Builder Pattern is a meal that can be purchased at many fast food franchises. The complex Product is a combo meal that consists of a burger, beverage and a side order. The Builder objects are the different assistants at the till that knows how to create the combo meal for the client. The Director object is the instructions the client gives the assistant on how the specific order should be created.

Example Combo Meal:

Download Combo Meal Example

The ComboMealClient class makes use of the ComboMealDirector and the ComboMeal1ConcreteBuilder class to create a complex object called ComboMealProduct.


ComboMeal1ConcreteBuilder concreteBuilder = new ComboMeal1ConcreteBuilder();
ComboMealDirector mealDirector = new ComboMealDirector(concreteBuilder);
ComboMealProduct comboMealProduct = null;

comboMealProduct = concreteBuilder.getComboMealProduct();

The ComboMealDirector class invokes the appropriate methods on the ConcreteBuilder (ComboMeal1ConcreteBuilder) to create a complex product ComboMealProduct.


public void constructComboMeal(SuperSize _mealSize) {
} // method constructComboMeal

The ComboMealProduct class is the complex object whose individual parts is created by the different Builder objects.

The ComboMealBuilder class contains the interface that is used by the ComboMealDirector to create the complex object.

The ComboMeal1ConcreteBuilder class contains implementation that is used by the ComboMealDirector to create the complex object.

Class Diagram Example

Design Pattern, Builder Pattern, GOF

Sequence Diagram

Builder Pattern - Sequence Diagram

Sequence Diagram by "Yanic Inghelbrecht" with Trace Modeler


  • Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides.
    Design Patterns: Elements of Reusable Object-Oriented Software. Addison
    Wesley, 1995

The Java Zone is brought to you in partnership with ZeroTurnaround. Discover how you can skip the build and redeploy process by using JRebel by ZeroTurnaround.


Published at DZone with permission of Andre Mare .

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}