Back to blog

Builder and Director Pattern

3 min
Builder and Director Pattern

The Builder and Director pattern is a creational design pattern that is used to build complex objects step by step. It provides a flexible way to create different variations of an object without compromising its internal structure. In this article, we will explore how this pattern is implemented and how it can benefit software development.

What is the Builder and Director pattern?

The Builder and Director pattern is based on the separation of the construction of a complex object from its representation. The objective is to allow the same construction process to create different representations of the object.

The pattern consists of two main components:

  1. Builder: defines an interface to build parts of the complex object.
  2. Director: uses the builder to build the final object step by step.

The idea behind the pattern is that the Director does not need to know the specific details of how the parts of the object are built. It simply uses the methods defined in the Builder interface to build the object step by step.

Example implementation in code

Suppose we have a Pizza class that can have different combinations of ingredients. We will use the Builder and Director pattern to build different types of pizzas in a flexible way.

class Pizza {
    crust: string;
    sauce: string;
    toppings: string[];

    description(): void {
        console.log(`Pizza with ${this.crust}, 
        ${this.sauce}, and ${this.toppings.join(", ")}`);
    }
}

class PizzaBuilder {
    pizza: Pizza;

    constructor() {
        this.pizza = new Pizza();
    }

    setCrust(crust: string): void {
        this.pizza.crust = crust;
    }

    setSauce(sauce: string): void {
        this.pizza.sauce = sauce;
    }

    addTopping(topping: string): void {
        this.pizza.toppings ||= [];
        this.pizza.toppings.push(topping);
    }
}

class PizzaDirector {
    buildMargheritaPizza(builder: PizzaBuilder): void {
        builder.setCrust("Thin crust");
        builder.setSauce("Tomato sauce");
        builder.addTopping("Cheese");
    }

    buildPepperoniPizza(builder: PizzaBuilder): void {
        builder.setCrust("Thick crust");
        builder.setSauce("Tomato sauce");
        builder.addTopping("Cheese");
        builder.addTopping("Pepperoni");
    }
}

// Using the Builder and Director pattern
const builder = new PizzaBuilder();
const director = new PizzaDirector();

director.buildMargheritaPizza(builder);
const margheritaPizza = builder.pizza;
margheritaPizza.description();

director.buildPepperoniPizza(builder);
const pepperoniPizza = builder.pizza;
pepperoniPizza.description();

Use cases

Building complex documents

Imagine you are developing an application that generates reports or complex documents, with sections, tables, charts, etc. The Builder and Director pattern can be used to build these documents step by step, where the Builder is responsible for creating the different parts of the document and the Director is responsible for coordinating the construction in a specific order.

Creating configurable objects

If you have objects with many configurable properties, the Builder and Director pattern can be used to provide a more flexible and readable way to configure those objects. The Builder is responsible for defining methods to set each of the properties and the Director coordinates the sequence in which the properties are set.

Data processing in stages

In certain cases, you may have a process that involves multiple stages of data processing, where each stage performs a specific transformation. The Builder and Director pattern can be used to build a processing flow step by step, where each Builder represents a processing stage and the Director is responsible for orchestrating the sequence of stages.

These are just some examples of additional use cases of the Builder and Director pattern in software development. The flexibility and separation of responsibilities that this pattern offers make it suitable for situations where complex objects need to be built gradually and flexibly.

Conclusions

The Builder and Director pattern is a powerful tool in software development that allows building complex objects step by step. With its focus on separating construction and representation, it offers flexibility and readability in the code. Additionally, it can be applied in a wide range of use cases.

I hope this article has given you a solid understanding of the Builder and Director pattern and how to use it in your software development! Try this pattern in your projects and discover its benefits for yourself.

Happy coding!

Share