Lesson 4
Builder Pattern in JavaScript
Builder Pattern Introduction

Welcome back! So far, we've covered various creational design patterns like the Singleton Pattern, Factory Method Pattern, and Abstract Factory Pattern. These patterns have helped you control and simplify object creation in your programs. Today, we are delving into another powerful creational pattern — the Builder Pattern. This pattern allows you to construct complex objects step by step, making the creation process more manageable and modular.

Defining the Builder Pattern

The Builder Pattern is a design pattern that provides a way to construct complex objects step by step. It decouples the construction process from the representation, allowing the same construction process to create different representations. This pattern includes several key components: the product (the complex object to be created), the builder interface (specifying the construction steps), one or more concrete builders (implementing the construction steps for different representations), and a director (managing the construction process).

Implementing a Concrete Builder and Using a Director

Let's break down the implementation of the Builder Pattern in JavaScript with an example. For this example, we will create different types of houses using various builders.

Defining the House Class

First, we define the House class, which will be our complex object:

JavaScript
1class House { 2 constructor() { 3 this.foundation = null; 4 this.structure = null; 5 this.roof = null; 6 } 7 8 setFoundation(foundation) { 9 this.foundation = foundation; 10 } 11 12 setStructure(structure) { 13 this.structure = structure; 14 } 15 16 setRoof(roof) { 17 this.roof = roof; 18 } 19 20 showHouse() { 21 console.log(`House with ${this.foundation}, ${this.structure}, and ${this.roof}.`); 22 } 23}

In this class, the House object has three main parts: the foundation, the structure, and the roof. We also have methods to set these parts and a method to display the house.

Creating the Builder Interface

JavaScript does not have native support for interfaces. Instead, we can document the expected methods a class should implement.

JavaScript
1// HouseBuilder Interface 2// Methods: 3// buildFoundation() 4// buildStructure() 5// buildRoof() 6// getHouse()

This interface specifies the methods that any concrete builder must implement. The buildFoundation, buildStructure, and buildRoof methods represent the steps to construct a house.

Implementing Concrete Builders

Now, we implement concrete builders following the HouseBuilder pattern. Let's start with the ConcreteHouseBuilder:

JavaScript
1class ConcreteHouseBuilder { 2 constructor() { 3 this.house = new House(); 4 } 5 6 buildFoundation() { 7 this.house.setFoundation("Concrete Foundation"); 8 } 9 10 buildStructure() { 11 this.house.setStructure("Concrete Structure"); 12 } 13 14 buildRoof() { 15 this.house.setRoof("Concrete Roof"); 16 } 17 18 getHouse() { 19 return this.house; 20 } 21}

The ConcreteHouseBuilder class constructs a house with a concrete foundation, structure, and roof. We also have similar builders for wooden and brick houses:

JavaScript
1class WoodenHouseBuilder { 2 constructor() { 3 this.house = new House(); 4 } 5 6 buildFoundation() { 7 this.house.setFoundation("Wooden Foundation"); 8 } 9 10 buildStructure() { 11 this.house.setStructure("Wooden Structure"); 12 } 13 14 buildRoof() { 15 this.house.setRoof("Wooden Roof"); 16 } 17 18 getHouse() { 19 return this.house; 20 } 21} 22 23class BrickHouseBuilder { 24 constructor() { 25 this.house = new House(); 26 } 27 28 buildFoundation() { 29 this.house.setFoundation("Brick Foundation"); 30 } 31 32 buildStructure() { 33 this.house.setStructure("Brick Structure"); 34 } 35 36 buildRoof() { 37 this.house.setRoof("Brick Roof"); 38 } 39 40 getHouse() { 41 return this.house; 42 } 43}

Each concrete builder creates a different type of house.

Using a Director to Manage the Construction Process

We introduce the Director class, which will manage the construction process:

JavaScript
1class Director { 2 constructor() { 3 this.builder = null; 4 } 5 6 setBuilder(builder) { 7 this.builder = builder; 8 } 9 10 constructHouse() { 11 this.builder.buildFoundation(); 12 this.builder.buildStructure(); 13 this.builder.buildRoof(); 14 return this.builder.getHouse(); 15 } 16}

The Director class holds a reference to a builder and manages the sequence of the building steps.

Example Usage

Here's how you can use the Director and ConcreteHouseBuilder to construct a house:

JavaScript
1const director = new Director(); 2const builder = new ConcreteHouseBuilder(); 3director.setBuilder(builder); 4 5const house = director.constructHouse(); 6house.showHouse(); 7// Output: House with Concrete Foundation, Concrete Structure, and Concrete Roof.

This script creates a director and a concrete house builder, sets the builder in the director, and constructs the house. Finally, it displays the constructed house.

Conclusion

The Builder Pattern is crucial for constructing complex objects in a controlled manner. By segmenting the construction process into distinct steps, you can more easily manage and update your code. The pattern allows for different representations of the object being constructed, enabling customization. Updating or changing the construction process is straightforward, as each step is encapsulated in methods.

These features make the Builder Pattern especially useful for constructing objects that require multiple configurations or assemblies, such as graphical user interfaces, parsing objects in data processing, or even complex game scenarios. By grasping the Builder Pattern, you'll enhance your ability to design flexible, maintainable, and scalable software architectures. Ready to dive in and get hands-on experience? Let's build something amazing together.

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.