Builder Design Pattern in Java with Examples | Beginner to Advanced Guide
Reading Time: 11–14 minutes
The Builder Design Pattern is a creational design pattern used to construct complex objects step by step. It separates the construction of an object from its representation, allowing the same construction process to create different representations.
The Builder pattern is very useful when a class has many parameters, especially optional parameters. It improves readability, flexibility, and maintainability of the code.
Why Do We Need Builder Pattern
Let us understand the problem first.
Problem with Multiple Constructor Parameters
Suppose we have a User class with many attributes:
package com.codekatha;
class User {
private String name;
private int age;
private String email;
private String phone;
private String address;
public User(String name, int age, String email, String phone, String address) {
this.name = name;
this.age = age;
this.email = email;
this.phone = phone;
this.address = address;
}
}
Now imagine some fields are optional. We may create multiple constructors:
public User(String name, int age) { }
public User(String name, int age, String email) { }
public User(String name, int age, String email, String phone) { }
This is called the telescoping constructor problem. The code becomes confusing and hard to maintain.
Problem with Setters Approach
Another approach is to use setters:
package com.codekatha;
class User {
private String name;
private int age;
private String email;
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setEmail(String email) {
this.email = email;
}
}
This approach has issues:
Object can be partially constructed
Object becomes mutable
Hard to enforce required fields
Thread safety issues
Solution – Builder Pattern
The Builder pattern allows step-by-step object construction. It ensures readability and better control over object creation.
Instead of creating objects using complex constructors, we use a builder.
Basic Structure of Builder Pattern
Product class with private constructor
Static inner Builder class
Builder methods to set fields
build() method to return final object
Step-by-Step Implementation
Step 1 – Create Product Class
package com.codekatha;
class User {
private final String name;
private final int age;
private final String email;
private final String phone;
private User(Builder builder) {
this.name = builder.name;
this.age = builder.age;
this.email = builder.email;
this.phone = builder.phone;
}
public String getName() {
return name;
}
public static class Builder {
private final String name;
private final int age;
private String email;
private String phone;
public Builder(String name, int age) {
this.name = name;
this.age = age;
}
public Builder email(String email) {
this.email = email;
return this;
}
public Builder phone(String phone) {
this.phone = phone;
return this;
}
public User build() {
return new User(this);
}
}
}
Step 2 – Using the Builder
package com.codekatha;
class BuilderDemo {
public static void main(String[] args) {
User user = new User.Builder("Advait", 25)
.email("advait@example.com")
.phone("9999999999")
.build();
System.out.println(user.getName());
}
}
What We Achieved Using Builder
Readable object creation
Optional parameters supported
Immutable object possible
Avoid telescoping constructors
Better maintainability
Understanding Builder with Real-Life Example
Think of ordering a burger.
Choose bun type
Add cheese
Add sauce
Add toppings
Build burger
You do not call a huge constructor with many parameters. You build it step by step.
Immutable Objects with Builder
Notice that in our example:
All fields are final
No setter methods
Object state cannot change after creation
This makes the class immutable and thread-safe.
Advantages of Builder Pattern
Improves readability
Supports optional parameters
Encapsulates object creation logic
Encourages immutability
Avoids constructor confusion
Disadvantages of Builder Pattern
More code required
Extra inner class needed
May be overkill for simple objects
When Should You Use Builder Pattern
When a class has many parameters
When some parameters are optional
When object creation is complex
When immutability is required
Builder vs Factory Pattern
Factory creates different types of objects.
Builder constructs complex objects step by step.
Factory focuses on object type, while Builder focuses on object construction.
Real-World Usage of Builder
Lombok @Builder annotation
StringBuilder class
HTTP request builders
Configuration objects
DTO creation in enterprise applications
Common Interview Questions on Builder
What problem does Builder solve?
Difference between Builder and Factory?
How Builder supports immutability?
When to avoid Builder?
Simple Summary
Builder constructs complex objects step by step.
Improves readability and maintainability.
Encourages immutable object design.
Ideal for objects with many optional parameters.
Conclusion
The Builder Design Pattern is an excellent solution for constructing complex objects in a clean and readable manner. It prevents constructor confusion, improves flexibility, and promotes immutability.
Understanding Builder helps in designing scalable enterprise systems and writing clean production-ready Java code.
Comments
Post a Comment