mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-29 03:00:15 +02:00
PHP7 Builder
This commit is contained in:
@@ -2,52 +2,36 @@
|
|||||||
|
|
||||||
namespace DesignPatterns\Creational\Builder;
|
namespace DesignPatterns\Creational\Builder;
|
||||||
|
|
||||||
/**
|
use DesignPatterns\Creational\Builder\Parts\Vehicle;
|
||||||
* BikeBuilder builds bike.
|
|
||||||
*/
|
|
||||||
class BikeBuilder implements BuilderInterface
|
class BikeBuilder implements BuilderInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var Parts\Bike
|
* @var Parts\Bike
|
||||||
*/
|
*/
|
||||||
protected $bike;
|
private $bike;
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function addDoors()
|
public function addDoors()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function addEngine()
|
public function addEngine()
|
||||||
{
|
{
|
||||||
$this->bike->setPart('engine', new Parts\Engine());
|
$this->bike->setPart('engine', new Parts\Engine());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function addWheel()
|
public function addWheel()
|
||||||
{
|
{
|
||||||
$this->bike->setPart('forwardWheel', new Parts\Wheel());
|
$this->bike->setPart('forwardWheel', new Parts\Wheel());
|
||||||
$this->bike->setPart('rearWheel', new Parts\Wheel());
|
$this->bike->setPart('rearWheel', new Parts\Wheel());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function createVehicle()
|
public function createVehicle()
|
||||||
{
|
{
|
||||||
$this->bike = new Parts\Bike();
|
$this->bike = new Parts\Bike();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function getVehicle(): Vehicle
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function getVehicle()
|
|
||||||
{
|
{
|
||||||
return $this->bike;
|
return $this->bike;
|
||||||
}
|
}
|
||||||
|
@@ -2,30 +2,17 @@
|
|||||||
|
|
||||||
namespace DesignPatterns\Creational\Builder;
|
namespace DesignPatterns\Creational\Builder;
|
||||||
|
|
||||||
|
use DesignPatterns\Creational\Builder\Parts\Vehicle;
|
||||||
|
|
||||||
interface BuilderInterface
|
interface BuilderInterface
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function createVehicle();
|
public function createVehicle();
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function addWheel();
|
public function addWheel();
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function addEngine();
|
public function addEngine();
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function addDoors();
|
public function addDoors();
|
||||||
|
|
||||||
/**
|
public function getVehicle(): Vehicle;
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getVehicle();
|
|
||||||
}
|
}
|
||||||
|
@@ -2,36 +2,26 @@
|
|||||||
|
|
||||||
namespace DesignPatterns\Creational\Builder;
|
namespace DesignPatterns\Creational\Builder;
|
||||||
|
|
||||||
/**
|
use DesignPatterns\Creational\Builder\Parts\Vehicle;
|
||||||
* CarBuilder builds car.
|
|
||||||
*/
|
|
||||||
class CarBuilder implements BuilderInterface
|
class CarBuilder implements BuilderInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var Parts\Car
|
* @var Parts\Car
|
||||||
*/
|
*/
|
||||||
protected $car;
|
private $car;
|
||||||
|
|
||||||
/**
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function addDoors()
|
public function addDoors()
|
||||||
{
|
{
|
||||||
$this->car->setPart('rightdoor', new Parts\Door());
|
$this->car->setPart('rightDoor', new Parts\Door());
|
||||||
$this->car->setPart('leftDoor', new Parts\Door());
|
$this->car->setPart('leftDoor', new Parts\Door());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function addEngine()
|
public function addEngine()
|
||||||
{
|
{
|
||||||
$this->car->setPart('engine', new Parts\Engine());
|
$this->car->setPart('engine', new Parts\Engine());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function addWheel()
|
public function addWheel()
|
||||||
{
|
{
|
||||||
$this->car->setPart('wheelLF', new Parts\Wheel());
|
$this->car->setPart('wheelLF', new Parts\Wheel());
|
||||||
@@ -40,18 +30,12 @@ class CarBuilder implements BuilderInterface
|
|||||||
$this->car->setPart('wheelRR', new Parts\Wheel());
|
$this->car->setPart('wheelRR', new Parts\Wheel());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function createVehicle()
|
public function createVehicle()
|
||||||
{
|
{
|
||||||
$this->car = new Parts\Car();
|
$this->car = new Parts\Car();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function getVehicle(): Vehicle
|
||||||
* @return Parts\Car
|
|
||||||
*/
|
|
||||||
public function getVehicle()
|
|
||||||
{
|
{
|
||||||
return $this->car;
|
return $this->car;
|
||||||
}
|
}
|
||||||
|
@@ -2,22 +2,17 @@
|
|||||||
|
|
||||||
namespace DesignPatterns\Creational\Builder;
|
namespace DesignPatterns\Creational\Builder;
|
||||||
|
|
||||||
|
use DesignPatterns\Creational\Builder\Parts\Vehicle;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Director is part of the builder pattern. It knows the interface of the builder
|
* 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.
|
* 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
|
* You can also inject many builders instead of one to build more complex objects
|
||||||
*/
|
*/
|
||||||
class Director
|
class Director
|
||||||
{
|
{
|
||||||
/**
|
public function build(BuilderInterface $builder): Vehicle
|
||||||
* The director don't know about concrete part.
|
|
||||||
*
|
|
||||||
* @param BuilderInterface $builder
|
|
||||||
*
|
|
||||||
* @return Parts\Vehicle
|
|
||||||
*/
|
|
||||||
public function build(BuilderInterface $builder)
|
|
||||||
{
|
{
|
||||||
$builder->createVehicle();
|
$builder->createVehicle();
|
||||||
$builder->addDoors();
|
$builder->addDoors();
|
||||||
|
@@ -2,9 +2,6 @@
|
|||||||
|
|
||||||
namespace DesignPatterns\Creational\Builder\Parts;
|
namespace DesignPatterns\Creational\Builder\Parts;
|
||||||
|
|
||||||
/**
|
|
||||||
* Bike is a bike.
|
|
||||||
*/
|
|
||||||
class Bike extends Vehicle
|
class Bike extends Vehicle
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@@ -2,9 +2,6 @@
|
|||||||
|
|
||||||
namespace DesignPatterns\Creational\Builder\Parts;
|
namespace DesignPatterns\Creational\Builder\Parts;
|
||||||
|
|
||||||
/**
|
|
||||||
* Car is a car.
|
|
||||||
*/
|
|
||||||
class Car extends Vehicle
|
class Car extends Vehicle
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@@ -2,19 +2,16 @@
|
|||||||
|
|
||||||
namespace DesignPatterns\Creational\Builder\Parts;
|
namespace DesignPatterns\Creational\Builder\Parts;
|
||||||
|
|
||||||
/**
|
|
||||||
* Vehicle class is an abstraction for a vehicle.
|
|
||||||
*/
|
|
||||||
abstract class Vehicle
|
abstract class Vehicle
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var object[]
|
||||||
*/
|
*/
|
||||||
protected $data;
|
private $data = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $key
|
* @param string $key
|
||||||
* @param mixed $value
|
* @param object $value
|
||||||
*/
|
*/
|
||||||
public function setPart($key, $value)
|
public function setPart($key, $value)
|
||||||
{
|
{
|
||||||
|
@@ -3,39 +3,24 @@
|
|||||||
namespace DesignPatterns\Creational\Builder\Tests;
|
namespace DesignPatterns\Creational\Builder\Tests;
|
||||||
|
|
||||||
use DesignPatterns\Creational\Builder\BikeBuilder;
|
use DesignPatterns\Creational\Builder\BikeBuilder;
|
||||||
use DesignPatterns\Creational\Builder\BuilderInterface;
|
|
||||||
use DesignPatterns\Creational\Builder\CarBuilder;
|
use DesignPatterns\Creational\Builder\CarBuilder;
|
||||||
use DesignPatterns\Creational\Builder\Director;
|
use DesignPatterns\Creational\Builder\Director;
|
||||||
|
|
||||||
/**
|
|
||||||
* DirectorTest tests the builder pattern.
|
|
||||||
*/
|
|
||||||
class DirectorTest extends \PHPUnit_Framework_TestCase
|
class DirectorTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
protected $director;
|
public function testCanBuildBike()
|
||||||
|
|
||||||
protected function setUp()
|
|
||||||
{
|
{
|
||||||
$this->director = new Director();
|
$bikeBuilder = new BikeBuilder();
|
||||||
|
$newVehicle = (new Director())->build($bikeBuilder);
|
||||||
|
|
||||||
|
$this->assertInstanceOf('DesignPatterns\Creational\Builder\Parts\Bike', $newVehicle);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getBuilder()
|
public function testCanBuildCar()
|
||||||
{
|
{
|
||||||
return array(
|
$carBuilder = new CarBuilder();
|
||||||
array(new CarBuilder()),
|
$newVehicle = (new Director())->build($carBuilder);
|
||||||
array(new BikeBuilder()),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
$this->assertInstanceOf('DesignPatterns\Creational\Builder\Parts\Car', $newVehicle);
|
||||||
* Here we test the build process. Notice that the client don't know
|
|
||||||
* anything about the concrete builder.
|
|
||||||
*
|
|
||||||
* @dataProvider getBuilder
|
|
||||||
*/
|
|
||||||
public function testBuild(BuilderInterface $builder)
|
|
||||||
{
|
|
||||||
$newVehicle = $this->director->build($builder);
|
|
||||||
$this->assertInstanceOf('DesignPatterns\Creational\Builder\Parts\Vehicle', $newVehicle);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user