Skip to main content

Command Palette

Search for a command to run...

What , Why & When Builder Pattern

Everything You Need to Know About the Builder Pattern

Updated
3 min read
What , Why & When Builder Pattern

Ever written a constructor so long it looked like a grocery list?
Yeah, me too.

You start with a simple class — maybe a User. It only needs a name and email.

Then the next feature arrives.

Now you need phone, address, gender, profile photo, preferences, and whatnot.

Before you realize it, your constructor looks like this 👇

User user = new User("Aditya", "aditya@gmail.com", "9876543210", "Male", "Bhopal",
 "image.png", true, false, LocalDate.now());

And the next developer who opens your class?
They’ll cry first, then quit. 😩

⚙️ The Problem: The Telescoping Constructor Nightmare

This is called the Telescoping Constructor Problem
when your class grows with too many optional parameters, you end up with:

  • Huge constructors

  • Confusing parameter order

  • Hard-to-read code

  • Nightmare when adding new fields

🧩 The Need for a Better Way

We need a way to create objects like:

User user = new UserBuilder()
    .setName("Aditya")
    .setEmail("aditya@gmail.com")
    .setPhone("9876543210")
    .build();

Clean. Readable. Flexible.
That’s where the Builder Pattern comes in.

What Is the Builder Pattern?

The Builder Pattern is a creational design pattern that lets you construct complex objects step by step.
It separates object construction from its representation, allowing you to create different types or variations of objects using the same construction process.

In simple words:

Instead of passing 10 parameters into a constructor,
you “build” the object step-by-step using meaningful methods.

❌ Without Builder Pattern (Constructor Hell)

public class User {
    private String name;
    private String email;
    private String phone;
    private String address;

    public User(String name, String email, String phone, String address) {
        this.name = name;
        this.email = email;
        this.phone = phone;
        this.address = address;
    }
}

Creating a user:

User user = new User("Aditya", "aditya@gmail.com", "989654XXXX", "Bhopal");

Now imagine you add 4 more parameters later. You’ll end up with constructor overloads and confusion.


✅ With Builder Pattern

public class User {
    private String name;
    private String email;
    private String phone;
    private String address;

    private User(UserBuilder builder) {
        this.name = builder.name;
        this.email = builder.email;
        this.phone = builder.phone;
        this.address = builder.address;
    }

    public static class UserBuilder {
        private String name;
        private String email;
        private String phone;
        private String address;

        public UserBuilder setName(String name) {
            this.name = name;
            return this;
        }

        public UserBuilder setEmail(String email) {
            this.email = email;
            return this;
        }

        public UserBuilder setPhone(String phone) {
            this.phone = phone;
            return this;
        }

        public UserBuilder setAddress(String address) {
            this.address = address;
            return this;
        }

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

Now creation looks like:

User user = new User.UserBuilder()
    .setName("Aditya Singh Rajput")
    .setEmail("aditya@gmail.com")
    .setPhone("9876543210")
    .setAddress("Bhopal")
    .build();

✅ Readable
✅ Flexible
✅ Easy to add new fields later


🏗️ Real-Life Use Case

Think about creating an Order or Product entity in an eCommerce system.
Some fields are mandatory, while others are optional (like discounts, coupons, etc.).

Using the Builder Pattern makes object creation clear, controlled, and scalable.
That’s why frameworks like Lombok’s , @Builder, Spring Boot DTOs, and even Java Streams internally follow similar principles.