It separates the object's construction from its representation, allowing the same construction process to create different object configurations.
The pattern is particularly useful when an object contains many optional fields or when creating the object through constructors becomes difficult due to numerous parameters.
Instead of exposing multiple constructors, the Builder Pattern provides a fluent API that lets developers specify only the required properties while keeping the object immutable and easy to read.
The Builder Pattern is widely used in modern Java applications to create immutable objects, configuration objects, DTOs, and complex domain models.
Structure
The Builder Pattern consists of the following participants.1. The Product is the complex object being created.
2. The Builder provides methods for configuring the object's properties.
3. The Client uses the builder to construct the desired object before calling build().

Java Implementation
Suppose an application needs to create employee records.Some fields such as employee ID and name are mandatory, while others such as department, email, phone number, and address are optional.
Create the product class.
public class Employee {
private final int id;
private final String name;
private final String department;
private final String email;
private final String phone;
private final String address;
private Employee(Builder builder) {
this.id = builder.id;
this.name = builder.name;
this.department = builder.department;
this.email = builder.email;
this.phone = builder.phone;
this.address = builder.address;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", department='" + department + '\'' +
", email='" + email + '\'' +
", phone='" + phone + '\'' +
", address='" + address + '\'' +
'}';
}
public static class Builder {
private final int id;
private final String name;
private String department;
private String email;
private String phone;
private String address;
public Builder(int id, String name) {
this.id = id;
this.name = name;
}
public Builder department(String department) {
this.department = department;
return this;
}
public Builder email(String email) {
this.email = email;
return this;
}
public Builder phone(String phone) {
this.phone = phone;
return this;
}
public Builder address(String address) {
this.address = address;
return this;
}
public Employee build() {
return new Employee(this);
}
}
}
The client creates the object using the builder.
public class Main {
public static void main(String[] args) {
Employee employee = new Employee.Builder(101, "John")
.department("Engineering")
.email("john@example.com")
.phone("9876543210")
.build();
System.out.println(employee);
}
}
Output:
Employee{id=101, name='John', department='Engineering',
email='john@example.com', phone='9876543210',
address='null'}
How It Works?
The client begins by creating a new Builder object with the mandatory fields required to construct an Employee.Each builder method configures one optional property and returns the same builder instance, allowing multiple method calls to be chained together in a fluent and readable manner.
When all required properties have been configured, the client invokes the build() method. The builder passes its values to the private constructor of the Employee class, which creates the final immutable object.
Since object creation is handled entirely by the builder, the product class does not require numerous overloaded constructors or setter methods.
Advantages
1. It simplifies the creation of complex objects with many optional properties.2. It produces more readable code through a fluent API.
3. It eliminates the need for telescoping constructors.
4. It supports immutable objects by initializing all fields during construction.
Disadvantages
1. It introduces additional classes and code compared to simple constructors.2. The pattern may be unnecessary for objects with only a few properties.
3. Maintaining the builder requires updating it whenever new fields are added to the product.
JDK Examples
The Builder Pattern is widely used throughout the Java ecosystem.1. java.net.http.HttpRequest.Builder constructs HTTP requests using a fluent API before creating the final immutable HttpRequest.
2. java.lang.StringBuilder incrementally builds complex strings before producing the final result using toString().
Summary
The Builder Pattern constructs complex objects step by step while separating object creation from the object's representation.By providing a fluent API and supporting immutable objects, the Builder Pattern improves readability, flexibility, and maintainability, making it one of the most commonly used creational design patterns in modern Java applications.