Перейти до основного вмісту

2. Будівельник (Builder)

Призначення

Будівельник - це інтерфейс для виробництва складних об'єктів.

Іноді, якщо Будівельник краще знає про те, що він будує, цей інтерфейс може бути абстрактним класом з методами за замовчуванням (адаптер).

Якщо у вас є складне дерево успадкування для об'єктів, логічно мати складне дерево успадкування і їх будівельників.

Примітка: Будівельники можуть мати текучий інтерфейсhttps://uk.wikipedia.org/wiki/Fluent_interface, наприклад, будівельник mock у PHPUnit.

Приклади

PHPUnit: Mock Builder

Діаграма UML

Builder UML

Код

Ви можете знайти цей код на GitHub

Director.php
<?php

declare(strict_types=1);

namespace DesignPatterns\Creational\Builder;

use DesignPatterns\Creational\Builder\Parts\Vehicle;

/**
* Director is part of the builder pattern. It knows the interface of the builder
* and builds a complex object with the help of the builder
*
* You can also inject many builders instead of one to build more complex objects
*/
class Director
{
public function build(Builder $builder): Vehicle
{
$builder->createVehicle();
$builder->addDoors();
$builder->addEngine();
$builder->addWheel();

return $builder->getVehicle();
}
}

Builder.php
<?php

declare(strict_types=1);

namespace DesignPatterns\Creational\Builder;

use DesignPatterns\Creational\Builder\Parts\Vehicle;

interface Builder
{
public function createVehicle(): void;

public function addWheel(): void;

public function addEngine(): void;

public function addDoors(): void;

public function getVehicle(): Vehicle;
}
TruckBuilder.php
<?php

declare(strict_types=1);

namespace DesignPatterns\Creational\Builder;

use DesignPatterns\Creational\Builder\Parts\Door;
use DesignPatterns\Creational\Builder\Parts\Engine;
use DesignPatterns\Creational\Builder\Parts\Wheel;
use DesignPatterns\Creational\Builder\Parts\Truck;
use DesignPatterns\Creational\Builder\Parts\Vehicle;

class TruckBuilder implements Builder
{
private Truck $truck;

public function addDoors(): void
{
$this->truck->setPart('rightDoor', new Door());
$this->truck->setPart('leftDoor', new Door());
}

public function addEngine(): void
{
$this->truck->setPart('truckEngine', new Engine());
}

public function addWheel(): void
{
$this->truck->setPart('wheel1', new Wheel());
$this->truck->setPart('wheel2', new Wheel());
$this->truck->setPart('wheel3', new Wheel());
$this->truck->setPart('wheel4', new Wheel());
$this->truck->setPart('wheel5', new Wheel());
$this->truck->setPart('wheel6', new Wheel());
}

public function createVehicle(): void
{
$this->truck = new Truck();
}

public function getVehicle(): Vehicle
{
return $this->truck;
}
}
CarBuilder.php
<?php

declare(strict_types=1);

namespace DesignPatterns\Creational\Builder;

use DesignPatterns\Creational\Builder\Parts\Door;
use DesignPatterns\Creational\Builder\Parts\Engine;
use DesignPatterns\Creational\Builder\Parts\Wheel;
use DesignPatterns\Creational\Builder\Parts\Car;
use DesignPatterns\Creational\Builder\Parts\Vehicle;

class CarBuilder implements Builder
{
private Car $car;

public function addDoors(): void
{
$this->car->setPart('rightDoor', new Door());
$this->car->setPart('leftDoor', new Door());
$this->car->setPart('trunkLid', new Door());
}

public function addEngine(): void
{
$this->car->setPart('engine', new Engine());
}

public function addWheel(): void
{
$this->car->setPart('wheelLF', new Wheel());
$this->car->setPart('wheelRF', new Wheel());
$this->car->setPart('wheelLR', new Wheel());
$this->car->setPart('wheelRR', new Wheel());
}

public function createVehicle(): void
{
$this->car = new Car();
}

public function getVehicle(): Vehicle
{
return $this->car;
}
}
Parts/Vehicle.php
<?php

declare(strict_types=1);

namespace DesignPatterns\Creational\Builder\Parts;

abstract class Vehicle
{
final public function setPart(string $key, object $value)
{
}
}
Parts/Truck.php
<?php

declare(strict_types=1);

namespace DesignPatterns\Creational\Builder\Parts;

class Truck extends Vehicle
{
}
Parts/Car.php
<?php

declare(strict_types=1);

namespace DesignPatterns\Creational\Builder\Parts;

class Car extends Vehicle
{
}
Parts/Engine.php
<?php

declare(strict_types=1);

namespace DesignPatterns\Creational\Builder\Parts;

class Engine
{
}
Parts/Wheel.php
<?php

declare(strict_types=1);

namespace DesignPatterns\Creational\Builder\Parts;

class Wheel
{
}
Parts/Door.php
<?php

declare(strict_types=1);

namespace DesignPatterns\Creational\Builder\Parts;

class Door
{
}

Тест

Tests/DirectorTest.php
<?php

declare(strict_types=1);

namespace DesignPatterns\Creational\Builder\Tests;

use DesignPatterns\Creational\Builder\Parts\Car;
use DesignPatterns\Creational\Builder\Parts\Truck;
use DesignPatterns\Creational\Builder\TruckBuilder;
use DesignPatterns\Creational\Builder\CarBuilder;
use DesignPatterns\Creational\Builder\Director;
use PHPUnit\Framework\TestCase;

class DirectorTest extends TestCase
{
public function testCanBuildTruck()
{
$truckBuilder = new TruckBuilder();
$newVehicle = (new Director())->build($truckBuilder);

$this->assertInstanceOf(Truck::class, $newVehicle);
}

public function testCanBuildCar()
{
$carBuilder = new CarBuilder();
$newVehicle = (new Director())->build($carBuilder);

$this->assertInstanceOf(Car::class, $newVehicle);
}
}