Php builder pattern without inner classes

Sometimes, if the builder has a better knowledge of what it builds, thisinterface could be an abstract class with mặc định methods (aka adapter).

If you have a complex inheritance tree for objects, it is logical tohave a complex inheritance tree for builders too.

Note: Builders have often a fluent interface, see the mock builder ofPHPUnit for example.


Bạn đang xem: Php builder pattern without inner classes

1.2.3. UML Diagram¶

*

declare(strict_types=1);namespace DesignPatternsCreationalBuilder;use DesignPatternsCreationalBuilderPartsVehicle;/** * 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 lớn build more complex objects */class Director public function build(Builder $builder): Vehicle $builder->createVehicle(); $builder->addDoors(); $builder->addEngine(); $builder->addWheel(); return $builder->getVehicle();
declare(strict_types=1);namespace DesignPatternsCreationalBuilder;use DesignPatternsCreationalBuilderPartsVehicle;interface Builder public function createVehicle(); public function addWheel(); public function addEngine(); public function addDoors(); public function getVehicle(): Vehicle;
declare(strict_types=1);namespace DesignPatternsCreationalBuilder;use DesignPatternsCreationalBuilderPartsDoor;use DesignPatternsCreationalBuilderPartsEngine;use DesignPatternsCreationalBuilderPartsWheel;use DesignPatternsCreationalBuilderPartsTruck;use DesignPatternsCreationalBuilderPartsVehicle;class TruckBuilder implements Builder private Truck $truck; public function addDoors() $this->truck->setPart('rightDoor', new Door()); $this->truck->setPart('leftDoor', new Door()); public function addEngine() $this->truck->setPart('truckEngine', new Engine()); public function addWheel() $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() $this->truck = new Truck(); public function getVehicle(): Vehicle return $this->truck;

Xem thêm: Bí Quyết Làm Sữa Chua Ngon Mịn Chuẩn Công Thức, Đơn Giản Tại Nhà

declare(strict_types=1);namespace DesignPatternsCreationalBuilder;use DesignPatternsCreationalBuilderPartsDoor;use DesignPatternsCreationalBuilderPartsEngine;use DesignPatternsCreationalBuilderPartsWheel;use DesignPatternsCreationalBuilderPartsCar;use DesignPatternsCreationalBuilderPartsVehicle;class CarBuilder implements Builder private car $car; public function addDoors() $this->car->setPart('rightDoor', new Door()); $this->car->setPart('leftDoor', new Door()); $this->car->setPart('trunkLid', new Door()); public function addEngine() $this->car->setPart('engine', new Engine()); public function addWheel() $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() $this->car = new Car(); public function getVehicle(): Vehicle return $this->car;
declare(strict_types=1);namespace DesignPatternsCreationalBuilderParts;abstract class Vehicle public function setPart(string $key, object $value)
declare(strict_types=1);namespace DesignPatternsCreationalBuilderParts;class Truck extends Vehicle
declare(strict_types=1);namespace DesignPatternsCreationalBuilderParts;class oto extends Vehicle