Java Builder Pattern
By AmarSivas | | Updated : 2021-12-12 | Viewed : 1527 times

The builder design pattern is one of the creational design patterns. You might aware of creational design patterns that all these patterns will be used to create and configure objects. Here we will focus on the builder diesign pattern in java.
Table of Contents:
Builder Design Pattern
Let's consider below given example used in one of our applications.
public class Laptop {
private final String processor;
private final Integer ram;
private final String storage;
private final Integer capacity;
}
In the Laptop class, it may have some mandatory fields and some optional fields. Values for these all fields should be passed while creating the object itself as it is an immutable class.
We need to create different types of constructors. Let's create all types of constructors to understand the problem.
private Laptop(String processor)
private Laptop(String processor, Integer ram)
private Laptop(String processor, Integer ram, String storage)
private Laptop(String processor, Integer ram, String storage, Integer capacity)
All these constructors will be a bit messy and code file size will be increased. So to overcome this problem we have a builder design pattern. Here we will make use of the method chain.
Method Chain
To complete the sequence of steps in the creation/making of some class object we can use a method chain. Method chain will be in one statement and it consists of many numbers methods. Notice each method's return type should be
For suppose to create the laptop with builder pattern we use method chain as given below.
new Laptop.LaptopBuilder().processor("Intel Core I5").ram(4).storage("SSD").capacity(500).build();
So a sequence of steps executes throughout this method chain and performed some actions to create a Laptop object here.
Builder Design Pattern Java Example
Now we will focus on the same Laptop builder example. Here we are going to create a builder class for Laptop assembling.
Rules for Builder design pattern
Create a Laptop class with a static builder class in it. And in builder class has a different method to chain all the required actions.
public class Laptop {
private final String processor;
private final Integer ram;
private final String storage;
private final Integer capacity;
public Laptop(LaptopBuilder laptopBuilder) {
this.processor = laptopBuilder.processor;
this.ram = laptopBuilder.ram;
this.storage = laptopBuilder.storage;
this.capacity = laptopBuilder.capacity;
}
//getters
public final static class LaptopBuilder {
private String processor;
private Integer ram;
private String storage;
private Integer capacity;
public LaptopBuilder processor(String processor) {
this.processor = processor;
return this;
}
public LaptopBuilder ram(Integer ram) {
this.ram = ram;
return this;
}
public LaptopBuilder storage(String storage) {
this.storage = storage;
return this;
}
public LaptopBuilder capacity(Integer capacity) {
this.capacity = capacity;
return this;
}
public Laptop build() {
return new Laptop(this);
}
}
}
Notice
The client for the Builder demonstration would be as given below.
public class LaptopBuilderDemo {
public static void main(String[] args) {
Laptop laptop = new Laptop.LaptopBuilder().processor("Intel Core I5").ram(4).storage("SSD").capacity(500).build();
System.out.println(laptop);
}
}
Code repo for this example is Java-Builder-Design-Pattern