DEV Community

Harsh Mange
Harsh Mange

Posted on • Originally published at harshmange.hashnode.dev on

What is Builder Design Pattern?

The Builder Design Pattern is a creational design pattern used to construct complex objects. It allows for the separation of object construction from its representation, resulting in a more flexible and modular design. In this blog post, we will discuss the Builder Design Pattern in detail, including its definition, purpose, and examples.

Definition

The Builder Design Pattern is a design pattern that separates the construction of a complex object from its representation. It allows for the creation of objects that are composed of multiple parts or have a complex hierarchy by using a step-by-step approach. The pattern provides a way to construct different representations of an object using the same construction process.

Purpose

The purpose of the Builder Design Pattern is to provide a flexible and modular way of creating complex objects. It allows for the creation of objects that have different representations without changing the construction process. The pattern also simplifies the creation of objects that have a complex hierarchy or are composed of multiple parts.

            +------------+
            | Director |
            +------------+
            | Construct()|
            +------------+
                  /\
                 / \
                / \
               / \
              / \
             / \
+--------------+ +--------------+
| Builder | | Product |
+--------------+ +--------------+
| buildPartA() | | parts: Part[]|
| buildPartB() | | addPart(Part)|
| getResult() | +--------------+
+--------------+
         /\
        / \
       / \
      / \
     / \
+--------------+
| Part |
+--------------+
| description |
+--------------+

Enter fullscreen mode Exit fullscreen mode

Example

Let's consider an example of a pizza restaurant that offers different types of pizzas with various toppings. We can use the Builder Design Pattern to create pizzas with different toppings.

First, we create an interface for the pizza builder:

public interface PizzaBuilder {
    public void buildDough();
    public void buildSauce();
    public void buildToppings();
    public Pizza getPizza();
}

Enter fullscreen mode Exit fullscreen mode

The PizzaBuilder interface specifies the methods that are required to build a pizza. These methods include building the dough, sauce, toppings, and getting the pizza.

Next, we create a concrete builder class that implements the PizzaBuilder interface:

public class PepperoniPizzaBuilder implements PizzaBuilder {
    private Pizza pizza;

    public PepperoniPizzaBuilder() {
        pizza = new Pizza();
    }

    public void buildDough() {
        pizza.setDough("thin crust");
    }

    public void buildSauce() {
        pizza.setSauce("tomato sauce");
    }

    public void buildToppings() {
        pizza.setToppings(Arrays.asList("mozzarella cheese", "pepperoni"));
    }

    public Pizza getPizza() {
        return pizza;
    }
}

Enter fullscreen mode Exit fullscreen mode

The PepperoniPizzaBuilder class is a concrete builder that implements the PizzaBuilder interface. It specifies how to build a pepperoni pizza by setting the dough, sauce, and toppings. The getPizza() method returns the pizza that was built.

Finally, we create a director class that uses the builder to create the pizza:

public class PizzaDirector {
    private PizzaBuilder pizzaBuilder;

    public PizzaDirector(PizzaBuilder pizzaBuilder) {
        this.pizzaBuilder = pizzaBuilder;
    }

    public void makePizza() {
        pizzaBuilder.buildDough();
        pizzaBuilder.buildSauce();
        pizzaBuilder.buildToppings();
    }
}

Enter fullscreen mode Exit fullscreen mode

The PizzaDirector class is responsible for creating the pizza using the PizzaBuilder interface. The makePizza() method calls the methods in the PizzaBuilder interface to construct the pizza.

Now, we can use the PizzaDirector and PizzaBuilder classes to create different types of pizzas:

public static void main(String[] args) {
    PizzaBuilder pepperoniPizzaBuilder = new PepperoniPizzaBuilder();
    PizzaDirector pizzaDirector = new PizzaDirector(pepperoniPizzaBuilder);

    pizzaDirector.makePizza();
    Pizza pepperoniPizza = pepperoniPizzaBuilder.getPizza();

    System.out.println(pepperoniPizza);
}

Enter fullscreen mode Exit fullscreen mode

The main method creates a PepperoniPizzaBuilder and a PizzaDirector. The makePizza() method is called on the PizzaDirector, which constructs the pizza using the PepperoniPizzaBuilder. Finally, the getPizza() method is called on the PepperoniPizzaBuilder to retrieve the pizza that was constructed.

Top comments (0)