Knowledge Guide
HomeOO & Low-Level DesignCreational

Builder Pattern

The Builder Design Pattern is a creational pattern used to construct complex objects step by step. It separates the construction of a complex object from its representation, allowing the same construction process to create different representations. This pattern is particularly useful when an object needs to be created with many possible configurations and combinations of its components.

Real-Life Example

Builder Pattern - Example
Builder Pattern - Example

To give you an idea of how this pattern works in the real world, let's consider building a house. You wouldn't start construction without a detailed plan, right? You'd need to figure out things like how many rooms you want, what type of materials to use, and how everything should be laid out. Similarly, the Builder Pattern can serve as a blueprint for your software object if it has lots of parameters, especially if many of them are optional.

Structure of Builder Pattern

The class diagram of Builder Pattern contains four types of classes:

Builder Pattern - Class Diagram
Builder Pattern - Class Diagram

How It Works

Implementation of Builder Pattern

This section will explain the implementation of Builder Pattern in four languages: C++, Java, Python, and JavaScript.

Think about a pizza class. Pizza can come in various sizes, shapes, and topping combinations. The Builder Pattern can simplify this procedure by eliminating the need for several constructors for each combination or making the user remember parameter order.

Pseudocode

Let's first discuss the pseudocode for the above-discussed Pizza example. Then we will look at the implementation of it in different languages.

CLASS Pizza: PRIVATE size, crust, toppings CONSTRUCTOR(size, crust, toppings): SET this.size = size SET this.crust = crust SET this.toppings = toppings METHOD setSize(size): SET this.size = size METHOD setCrust(crust): SET this.crust = crust METHOD setToppings(toppings): SET this.toppings = toppings METHOD showPizza(): PRINT "Pizza Size:", size, ", Crust:", crust, ", Toppings:", toppings ENDCLASS ABSTRACT CLASS PizzaBuilder: PROTECTED pizza METHOD getPizza(): RETURN pizza METHOD createNewPizzaProduct(): SET pizza = NEW Pizza("default", "default", "default") ABSTRACT METHOD buildSize() ABSTRACT METHOD buildCrust() ABSTRACT METHOD buildToppings() ENDCLASS CLASS HawaiianPizzaBuilder EXTENDS PizzaBuilder: OVERRIDE METHOD buildSize(): SET pizza.size = "Large" OVERRIDE METHOD buildCrust(): SET pizza.crust = "Thin" OVERRIDE METHOD buildToppings(): SET pizza.toppings = "Ham and Pineapple" ENDCLASS CLASS Waiter: PRIVATE PizzaBuilder pizzaBuilder METHOD setPizzaBuilder(builder): SET pizzaBuilder = builder METHOD getPizza(): RETURN pizzaBuilder.getPizza() METHOD constructPizza(): CALL pizzaBuilder.createNewPizzaProduct() CALL pizzaBuilder.buildSize() CALL pizzaBuilder.buildCrust() CALL pizzaBuilder.buildToppings() ENDCLASS MAIN: DECLARE waiter = NEW Waiter DECLARE hawaiianPizzaBuilder = NEW HawaiianPizzaBuilder CALL waiter.setPizzaBuilder(hawaiianPizzaBuilder) CALL waiter.constructPizza() DECLARE pizza = waiter.getPizza() CALL pizza.showPizza() ENDMAIN
java
// The final product - Pizza
class Pizza {
    private String size;
    private String crust;
    private String toppings;

    // Constructor to initialize the pizza
    public Pizza(String size, String crust, String toppings) {
        this.size = size;
        this.crust = crust;
        this.toppings = toppings;
    }

    // Setter methods
    public void setSize(String size) {
        this.size = size;
    }

    public void setCrust(String crust) {
        this.crust = crust;
    }

    public void setToppings(String toppings) {
        this.toppings = toppings;
    }

    // Display the pizza properties
    public void showPizza() {
        System.out.println("Pizza Size: " + size + ", Crust: " + crust + ", Toppings: " + toppings);
    }
}

// Abstract builder class for pizza
abstract class PizzaBuilder {
    protected Pizza pizza;

    public Pizza getPizza() {
        return pizza;
    }

    public void createNewPizzaProduct() {
        pizza = new Pizza("default", "default", "default");
    }

    public abstract void buildSize();
    public abstract void buildCrust();
    public abstract void buildToppings();
}

// Concrete builder class for Hawaiian Pizza
class HawaiianPizzaBuilder extends PizzaBuilder {
    @Override
    public void buildSize() {
        pizza.setSize("Large");
    }

    @Override
    public void buildCrust() {
        pizza.setCrust("Thin");
    }

    @Override
    public void buildToppings() {
        pizza.setToppings("Ham and Pineapple");
    }
}
// Director class to construct the pizza
class Waiter {
    private PizzaBuilder pizzaBuilder;

    public void setPizzaBuilder(PizzaBuilder pb) {
        pizzaBuilder = pb;
    }

    public Pizza getPizza() {
        return pizzaBuilder.getPizza();
    }

    public void constructPizza() {
        pizzaBuilder.createNewPizzaProduct();
        pizzaBuilder.buildSize();
        pizzaBuilder.buildCrust();
        pizzaBuilder.buildToppings();
    }
}

// Main execution class
public class Solution {
    public static void main(String[] args) {
        Waiter waiter = new Waiter();
        PizzaBuilder hawaiianPizzaBuilder = new HawaiianPizzaBuilder();

        waiter.setPizzaBuilder(hawaiianPizzaBuilder);
        waiter.constructPizza();

        Pizza pizza = waiter.getPizza();
        pizza.showPizza();
    }
}

Applications of Builder Pattern

Let's discuss some of the example scenarios where Builder Pattern can help us.

These are some example scenarios in which Builder Pattern can be used to increase the system efficiency.

Pros and Cons

ProsCons
Separation of Construction and Representation: Allows you to construct complex objects step by step.Complexity: Introduces multiple additional classes, which increases the complexity of the code.
Encapsulation: Encapsulates the construction logic of an object, which can simplify the client code.Redundancy: For simpler objects, the builder pattern might be overkill, leading to unnecessary redundancy.
Control over Object Construction Process: The construction process can be controlled more finely than with other creation patterns.Specificity: Each different type of object requires a new concrete builder.
Immutability: Can be used to build immutable objects without needing a large number of constructor parameters.Understanding: Can be harder to understand and implement properly, especially for new developers.
Fluent Interfaces: Can provide a fluent API to improve readability and ease of use of object creation.Duplication: Sometimes there can be duplication of code in the builders if there is overlap in the parts they assemble.

Summary

The Builder Design Pattern is ideal for constructing complex objects, especially when the construction process must allow different representations or configurations of the object. It provides clarity and flexibility in how objects are constructed and is particularly useful in cases where objects need to be created with a large number of optional components or configurations. However, greater complexity and the possibility of duplicate code come at the expense of this flexibility and control.

🤖 Don't fully get this? Learn it with Claude

Stuck on Builder Pattern? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Builder Pattern** (OO & Low-Level Design) and want to truly understand it. Explain Builder Pattern from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Builder Pattern** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Builder Pattern** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Builder Pattern** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes