Builder Design Pattern

Builder is a creational design pattern that lets you construct complex objects step by step. The pattern allows you to produce different types and representations of an object using the same construction code. It is one of the most important and simple design pattern in java. The builder pattern helps to creating immutable classes with a large set of state attributes.

Representation: This step consists of how our product looks at the end when it is ready. In this step we combine the already constructed parts.

Construction: whereas, this step means how the parts of the products are actually made. This may include multiple steps.

Builder pattern separates these 2 steps so that we can create different type of products using the same set of steps.

The Builder design pattern solves problems like:

  • How can a class (the same construction process) create different representations of a complex object?
  • How can a class that includes creating a complex object be simplified?

In builder design pattern it has 3 component

  1. Product : This part of the code is the actual object that we are trying to build.
  2. Builder : It will contain the general methods needed to build the product.
  3. Executor : This is the important part which actually calls the builder methods to create the ‘Product’.

Implementation

Lets consider we want to create new cars according to name, color, price and year. In this example Product and Builder are in the same class.

Step 1:

Create the Car.java Class with the constructor that take attribute of CarBuilder class. In this example we will create static CarBuilder class in the Car.java class. You can create separate class for CarBuilder as well.

public class Car { 

private String name;
private double price;
private int year;
private Color color;
public Car(CarBuilder builder){
this.name = builder.name;
this.price = builder.price;
this.year = builder.year;
this.color = builder.color;
}

static class CarBuilder{

private final String name; //required
private double price;
private int year;
private Color color;

public CarBuilder(String name){
this.name = name;
}

public CarBuilder price(double price){
this.price = price;
return this;
}

public CarBuilder year(int year){
this.year = year;
return this;
}

public CarBuilder color(Color color){
this.color = color;
return this;
}
public CarBuilder build() {
return this;
}
}
@Override
public String toString() {
return "Car [name=" + name + ",\n price=" + price + ",\n year=" + year + ",\n color=" + color
+ "]";
}
}

Step 2

Create enum class for the colors. In here colors are pre-defined.

public enum Color {
Black,Blue,Green,Grey,Red,White,Yellow\
}

Step 3

Create the main method for the builder. Executor.

public class App {
public static void main(String[] args) {
Car car= null;
Car car1= null;

car = new Car(
new CarBuilder("Tesla Model 5")
.color(Color.White)
.price(70000)
.year(2014)
.build());

car1 = new Car(
new CarBuilder("Tesla Model 7")
.color(Color.Blue)
.price(90000)
.year(2015)
.build());

System.out.println(car);
System.out.println(car1);
}
}

Output

Car [name=Tesla Model 5,
price=70000.0,
year=2014,
color=White]
Car [name=Tesla Model 7,
price=90000.0,
year=2015,
color=Blue]

Summary

  • Builder design pattern is one of the structural design pattern
  • Separates the construction of a complex object from its representation
  • Simplifies the creation of complex object
  • Different representation of object is possible
  • Allows you to vary a product’s internal representation.
  • Encapsulates code for construction and representation.
  • Provides control over steps of construction process.
  • Data members of class aren’t guaranteed to be initialized.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store