The builder design pattern is a creative design pattern that can be used in cases when we need to create special class objects that are flexible and complex with the possibilities of dynamically setting properties and objects.
In this pattern, a director and a builder work together to build an object. The director controls the building and indicates what components and variations will go into an object. The builder knows how to assemble the specification given.
Structure:
- Concrete class
- Builder Interface
- Different Builder Implementations
- Director
- Client code
Lets see how builder will look like with some simple code examples:
- Concrete class(Vehicle)
<?php
// Concrete Class
class Vehicle
{
public $model;
public $engines_count;
public $type;
const CAR = "Car";
const BUS = "Bus";
public function __construct(){
}
} - Now, we will create the builder interface named “VehicleBuilderInterface” to set each component of the vehicle object creation. The method getVehicle will return the final object.
// Builder interface
interface VehicleBuilderInterface
{
public function setVehicleModel();
public function setEnginesCount();
public function setVehicleType();
public function getVehicle();
} - Now, we will implement the different builders by extending the Builder interface. As you see, We are passing the original concrete class(Vehicle) in every constructor. Then we are calling the helper functions such as setModel, and setType to set the properties of the object. In the end, we called getVehicle to return the Vehicle object.
class HondaCityCarBuilder implements VehicleBuilderInterface
{
private $vehicle;
public function __construct(Vehicle $vehicle)
{
$this->vehicle = $vehicle;
}
public function setVehicleModel()
{
$this->vehicle->model = "Honda City";
}
public function setEnginesCount()
{
$this->vehicle->enginesCount = 1;
}
public function setVehicleType()
{
$this->vehicle->type = Vehicle::CAR;
}
public function getVehicle()
{
return $this->vehicle;
}
}
class MahindraBusBuilder implements VehicleBuilderInterface
{
private $vehicle;
public function __construct(Vehicle $vehicle)
{
$this->vehicle = $vehicle;
}
public function setVehicleModel()
{
$this->vehicle->model = "Mahindra";
}
public function setEnginesCount()
{
$this->vehicle->enginesCount = 2;
}
public function setVehicleType()
{
$this->vehicle->type = Vehicle::BUS;
}
public function getVehicle()
{
return $this->vehicle;
}
} - A director class is for creating an object using the builder interface. It is only responsible for running the building steps in a specific order. The director’s main responsibility is to get a builder interface and call the builder’s methods and then retrieve the object.
class VehicleDirector
{
public function build(VehicleBuilderInterface $builder)
{
$builder->setVehicleModel();
$builder->setEnginesCount();
$builder->setVehicleType();
return $builder->getVehicle();
}
} - Client code: The client code creates a builder object, and sends it to the director. Then the director starts the construction process. The final result is obtained from the builder object.
$hondaCityCar = (new VehicleDirector())->build(new HondaCityCarBuilder(new Vehicle()));
$mahindraBus = (new VehicleDirector())->build(new MahindraBusBuilder(new Vehicle()));
Output:
echo "<pre>";
var_dump($hondaCityCar);
echo "\n";
echo "Vehicle Model: " . $hondaCityCar->model . "\n";
echo "Vehicle Engines: " . $hondaCityCar->engines_count . "\n";
echo "Vehicle Type: " . $hondaCityCar->type . "\n";
jQuery presents a tree-like structure of all the elements on a webpage simplifying the syntax and further manipulating such elements. The jQuery Certification exam by StudySection will secure your fundamental knowledge and a basic understanding of jQuery as an asset to improve your skills.