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