In this lesson, you will learn how to implement the Builder Pattern in PHP. Specifically, you'll understand how to:
- Define the Builder Pattern: Recognize the core components and when to use this pattern.
- Implement a Concrete Builder: See how to create concrete builders for constructing complex objects.
- 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
// 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
// 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
// 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
// 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
// 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
// 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.