Implementing the Builder Pattern in PHP

Builder Pattern Introduction

Welcome back! So far, we've covered various creational design patterns like the Singleton Pattern and the Factory Method Pattern. These patterns have helped you control and simplify object creation in your applications. 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. By using PHP, we can effectively employ the Builder Pattern to manage the creation of intricate objects in a server-side context, often essential in web development.

What You'll Learn

In this lesson, you will learn how to implement the Builder Pattern in PHP. Specifically, you'll understand how to:

  1. Define the Builder Pattern: Recognize the core components and when to use this pattern.
  2. Implement a Concrete Builder: See how to create concrete builders for constructing complex objects.
  3. Use a Director: Use a Director class to manage the construction process.

Here's a snippet of the code you'll work with:

The House.php file to define the House class — the product being constructed:

PHP
<?php

// Product House that will be constructed using builder pattern
class House {
    private $foundation;
    private $structure;
    private $roof;

    // Setters for the house properties - foundation, structure, and roof
    public function setFoundation($foundation) {
        $this->foundation = $foundation;
    }

    public function setStructure($structure) {
        $this->structure = $structure;
    }

    public function setRoof($roof) {
        $this->roof = $roof;
    }

    // Display the house properties
    public function showHouse() {
        echo "House with {$this->foundation}, {$this->structure}, and {$this->roof}.\n";
    }
}

The HouseBuilder.php file to define the HouseBuilder interface:

PHP
<?php

// Abstract class for HouseBuilder with methods to build foundation, structure, and roof
interface HouseBuilder {
    public function buildFoundation();
    public function buildStructure();
    public function buildRoof();
    public function getHouse();
}

The ConcreteHouseBuilder.php file to implement the HouseBuilder interface:

PHP
<?php

// Concrete builder class for constructing a house with concrete foundation, structure, and roof
class ConcreteHouseBuilder implements HouseBuilder {
    private $house;

    public function __construct() {
        $this->house = new House();
    }

    public function buildFoundation() {
        $this->house->setFoundation("Concrete Foundation");
    }

    public function buildStructure() {
        $this->house->setStructure("Concrete Structure");
    }

    public function buildRoof() {
        $this->house->setRoof("Concrete Roof");
    }

    public function getHouse() {
        return $this->house;
    }
}

The WoodenHouseBuilder.php to implement the HouseBuilder interface:

PHP
<?php

// Concrete builder class for constructing a house with wooden foundation, structure, and roof
class WoodenHouseBuilder implements HouseBuilder {
    private $house;

    public function __construct() {
        $this->house = new House();
    }

    public function buildFoundation() {
        $this->house->setFoundation("Wooden Foundation");
    }

    public function buildStructure() {
        $this->house->setStructure("Wooden Structure");
    }

    public function buildRoof() {
        $this->house->setRoof("Wooden Roof");
    }

    public function getHouse() {
        return $this->house;
    }
}

The BrickHouseBuilder.php to implement the HouseBuilder interface:

PHP
<?php

// Concrete builder class for constructing a house with brick foundation, structure, and roof
class BrickHouseBuilder implements HouseBuilder {
    private $house;

    public function __construct() {
        $this->house = new House();
    }

    public function buildFoundation() {
        $this->house->setFoundation("Brick Foundation");
    }

    public function buildStructure() {
        $this->house->setStructure("Brick Structure");
    }

    public function buildRoof() {
        $this->house->setRoof("Brick Roof");
    }

    public function getHouse() {
        return $this->house;
    }
}

The director.php file to demonstrate the Builder Pattern:

PHP
<?php

// Director class to manage the construction process by encapsulating the steps within the `constructHouse` method
class Director {
    private $builder;

    public function setBuilder(HouseBuilder $builder) {
        $this->builder = $builder;
    }

    public function constructHouse() {
        $this->builder->buildFoundation();
        $this->builder->buildStructure();
        $this->builder->buildRoof();
        return $this->builder->getHouse();
    }
}

$director = new Director(); // Director to manage the construction process
$builder = new BrickHouseBuilder(); // Concrete builder to build a brick house
$director->setBuilder($builder); // Set the builder to construct a brick house
$house = $director->constructHouse(); // Construct the house
$house->showHouse();

This example demonstrates how to use the Builder Pattern to create a House object step by step.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal