Builder Pattern
Builder Design Pattern using an inner static class is a common and clean way to create immutable objects in Java, especially when constructors become complex (many parameters, optional fields, etc.).
Get Syed Nazeer’s stories in your inbox
Join Medium for free to get updates from this writer.
Employee class using Builder Pattern:
package org.wipro.dp.builder;
public class User {
private final Integer age;
private final String name;
private final String phone;
private final String email;
private User(Builder builder) {
this.age = builder.age;
this.name = builder.name;
this.phone = builder.phone;
this.email = builder.email;
}
public Integer getAge() {
return this.age;
}
public String getName() {
return this.name;
}
public String getPhone() {
return this.phone;
}
public String getEmail() {
return this.email;
}
public static class Builder {
private Integer age;
private String name;
private String phone;
private String email;
public Builder(Integer age, String name) {
this.age = age;
this.name = name;
}
public Builder phone(String phone) {
this.phone = phone;
return this;
}
public Builder email(String email) {
this.email = email;
return this;
}
public User build() {
return new User(this);
}
}
@Override
public String toString() {
return "User{" +
"id=" + age +
", name='" + name + '\'' +
", salary=" + phone +
", address='" + email + '\'' +
'}';
}
}Client: We create multiple Employee objects passing different properties.
package org.wipro;
import org.wipro.dp.Singleton;
import org.wipro.dp.builder.Employee;
public class App {
public static void main(String[] args) {
User u1 = new User.Builder(10, "Syed Nazeer").phone("1234").email("Kodathi").build();
User u2 = new User.Builder(10, "Syed Nazeer").phone("1234").build();
User u3 = new User.Builder(10, "Syed Nazeer").email("Kodathi").build();
System.out.println(u1.toString());
System.out.println(u2.toString());
System.out.println(u3.toString());
}
}Output:
User{id=10, name='Syed Nazeer', salary=1234, address='Kodathi'}
User{id=10, name='Syed Nazeer', salary=1234, address='null'}
User{id=10, name='Syed Nazeer', salary=null, address='Kodathi'}UML Diagram:
+-----------------------------+
| User |
+-----------------------------+
| - name : String |
| - age : int |
| - email : String |
| - phone : String |
+-----------------------------+
| + getName() : String |
| + getAge() : int |
| + getEmail() : String |
| + getPhone() : String |
+-----------------------------+
| <<private>> User(Builder) |
+-----------------------------+
▲
|
|
+-----------------------------+
| User.Builder (static) |
+-----------------------------+
| - name : String |
| - age : int |
| - email : String |
| - phone : String |
+-----------------------------+
| + Builder(name, age) |
| + email(String) : Builder |
| + phone(String) : Builder |
| + build() : User |
+-----------------------------+Key UML Relationships
1. Composition / Tight Association
Useris constructed using Builder- Builder is a static inner class
- Represented by logical containment (inner class)
Mermaid UML Diagram (Visual)
Interview Explanation (How to Describe UML)
“User is an immutable class with private fields and no setters. It has a private constructor that accepts a Builder object. The Builder is a static inner class responsible for step-by-step object construction. The build() method returns the final User object.”
Key UML Points to Highlight
- Encapsulation → fields are private
- Immutability → no setters in
User - Builder static inner class
- Fluent interface (methods return
Builder) - Object creation delegated to Builder







