Builder Design Pattern in Java

Chakresh Tiwari
ShoutLoudz
Published in
3 min readDec 30, 2022
Builder pattern in java
Photo by Vrlibs Studio on Unsplash

Previously we have talked about What is design Patterns and their types?
https://medium.com/shoutloudz/low-level-design-intro-to-design-patterns-565214b79b0f

Introduction

In this story, we are going to discuss builder design patterns. Builder design patterns fall into the category of creational design patterns. As per the use cases which I have gone through I found that a builder pattern should be used when there are objects with a lot of properties and all are not necessary every time while creating the Objects.

In any Java class, there will be two kinds of fields one which is mandatory and another which are not mandatory(like they are not necessary to have values every time we create the object)

We can create an object by creating a constructor but calling the constructor every time for some unused fields will be problematic. we need to pass null in place of those values.

Advantages of Builder Pattern

  1. Builder pattern helps us achieve immutability. (we do not add a setter method in class so the values of the class can not be changed). we can think of a String class in java which is immutable.
  2. If we use the Builder pattern the code will be a bit long but that is good for complex object creation.
  3. It removed the headache of creating multiple constructors for creating a different kinds of objects.

A disadvantage of Builder Pattern

The only disadvantage of the builder pattern is it doubles the line of code.

Example

In this example, I am going to use an Employee class, which will have some optional fields.

public class Employee {

private int id;
private String name;
private int age;

//optional
private int salary;

public int getId() {
return id;
}

public String getName() {
return name;
}

public int getAge() {
return age;
}

public int getSalary() {
return salary;
}

private Employee(EmployeeBuilder builder) {
this.id = builder.id;
this.name = builder.name;
this.age = builder.age;
this.salary = builder.salary;
}

public static class EmployeeBuilder {

private int id;
private String name;
private int age;

//optional
private int salary;

public EmployeeBuilder setSalary(int salary) {
this.salary = salary;
return this;
}

public EmployeeBuilder(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}

public Employee build() {
return new Employee(this);
}

}

public static void main(String[] args) {

Employee e = new Employee.EmployeeBuilder(1, "test", 26).setSalary(22).build();
}

}

In the code, we can see, there are no setter methods( to make the class immutable ) constructor is private so no one can access the class directly. and EmployeeBuilder is within Employee so that when someone adds a new field to the employee they do not forget to update the builder class as well.

All the optional fields will have setter methods in the builder class and that will be used when creating an object.

It uses the concept of chaining while creating the object, Method chaining is also a similar concept.

That’s all for the builder design pattern, lets's try to use this pattern whenever any scenario comes when we have to create a class which will be used in multiple places with different kind of properties.

Thanks for reading!!

--

--

Chakresh Tiwari
ShoutLoudz

Software Engineer at Cisco(Appdynamics) , Sharing my knowledge and experience related to work. I am here to help learners to prepare for tech interviews.