Builder Design Pattern in PHP

1. Definition

The Builder Design Pattern separates the construction of a complex object from its representation, allowing the same construction process to create different representations.

2. Problem Statement

Imagine you want to build a house. A house can have a variety of components such as walls, doors, windows, and a roof. Directly building a house with various configurations can be complex and error-prone due to the numerous possibilities.

3. Solution

The Builder pattern suggests separating the construction of an object from its representation. This way, a dedicated 'builder' is responsible for piecing the objects together step by step. The 'director' specifies the building process but leaves the construction to the builder.

4. Real-World Use Cases

1. Constructing complex meal combos at a fast-food restaurant.

2. Assembling a computer with different configurations based on user requirements.

5. Implementation Steps

1. Define a Builder interface that specifies steps to construct parts of the product.

2. Implement concrete builders for different representations.

3. Define a Director class that constructs an object using the Builder interface.

4. The client will use the Director and the Builder to construct the desired object.

6. Implementation in PHP

<?php
// Step 1: Define the Builder interface
interface HouseBuilder {
    public function buildWalls();
    public function buildDoors();
    public function buildWindows();
    public function getHouse();
}
// Step 2: Implement concrete builder
class ConcreteHouseBuilder implements HouseBuilder {
    private $house = "";
    public function buildWalls() {
        $this->house .= "Concrete Walls, ";
    }
    public function buildDoors() {
        $this->house .= "Wooden Doors, ";
    }
    public function buildWindows() {
        $this->house .= "Glass Windows, ";
    }
    public function getHouse() {
        return rtrim($this->house, ', ');
    }
}
// Step 3: Define a Director class
class HouseDirector {
    public function buildHouse(HouseBuilder $builder) {
        $builder->buildWalls();
        $builder->buildDoors();
        $builder->buildWindows();
        return $builder->getHouse();
    }
}
// Client code
$builder = new ConcreteHouseBuilder();
$director = new HouseDirector();
$house = $director->buildHouse($builder);
echo $house;
?>

Output:

Concrete Walls, Wooden Doors, Glass Windows

Explanation:

We first defined a HouseBuilder interface that describes the steps to construct the parts of a house. The ConcreteHouseBuilder implements this interface, providing concrete implementations for each step.

The HouseDirector class, which represents the Director, is responsible for executing the building steps in the correct order. The client uses both the Director and the Builder to get the desired house configuration.

By separating the construction from its representation, the builder pattern provides flexibility and clarity in creating complex objects.

7. When to use?

Use the Builder Pattern when:

1. The algorithm for creating a complex object should be independent of the parts that make up the object and how they are assembled.

2. The construction process must allow different representations of the constructed object.

3. You want to provide the user with a clear way to create complex objects step by step.


Comments