Observe these two examples of Object Creation from the Student Class
Student student1 = new Student.Builder("Alice")
.age(12)
.grade("7th")
.email("alice@email.com")
.build();
Student student2 = new Student.Builder("Bob")
.grade("5th")
.build();
We did not call the constructor with the property values, but we built the object step by step with only the properties and values we needed.
This can be useful when we have classes with many properties, many properties/attributes are also optional or serve a specific purpose. Now, we can create an object dynamically with only the properties and values we need.
This is the Builder Design pattern.
From AI Tools/ OpenAI/Iternet
“Got it 👍 — here’s a copyright-free explanation of the Builder Pattern in Java that you can post directly on Facebook or your blog.
🏗️ Java Design Patterns – Builder Pattern
🔹 Definition
The Builder Pattern is a creational design pattern used to construct complex objects step by step. Instead of writing a long constructor with many parameters, the builder lets you create an object in a readable and flexible way.
🔹 Use Cases
You should use the Builder Pattern when:
- An object has many optional or mandatory fields.
- You want to avoid long constructors with many arguments.
- You need to make object creation more readable and less error-prone.
- The same object can be built in different configurations.
🔹 Advantages
✔️ Improves readability of code.
✔️ Makes objects immutable (if setters are avoided).
✔️ Reduces risk of errors when passing many arguments.
✔️ Allows step-by-step construction of objects.
✔️ Provides flexibility for creating different versions of the same object.
🔹 Example in Java
// Product class with Builder
class Student {
private String name;
private int age;
private String grade;
private String email;
// Private constructor
private Student(Builder builder) {
this.name = builder.name;
this.age = builder.age;
this.grade = builder.grade;
this.email = builder.email;
}
// Static inner Builder class
public static class Builder {
private String name;
private int age;
private String grade;
private String email;
public Builder(String name) { // mandatory field
this.name = name;
}
public Builder age(int age) {
this.age = age;
return this;
}
public Builder grade(String grade) {
this.grade = grade;
return this;
}
public Builder email(String email) {
this.email = email;
return this;
}
public Student build() {
return new Student(this);
}
}
@Override
public String toString() {
return "Student: " + name + ", Age: " + age + ", Grade: " + grade + ", Email: " + email;
}
}
// Usage
public class BuilderPatternExample {
public static void main(String[] args) {
Student student1 = new Student.Builder("Alice")
.age(12)
.grade("7th")
.email("alice@email.com")
.build();
Student student2 = new Student.Builder("Bob")
.grade("5th")
.build();
System.out.println(student1);
System.out.println(student2);
}
}
✅ Output:
Student: Alice, Age: 12, Grade: 7th, Email: alice@email.com
Student: Bob, Age: 0, Grade: 5th, Email: null
📌 In short:
The Builder Pattern is perfect when creating objects with many optional fields. It improves clarity, maintains immutability, and makes your code cleaner than using constructors with long parameter lists.