mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-02-21 08:02:34 +01:00
#226 fixed Builder
This commit is contained in:
parent
8aa780fa4d
commit
79f25f8442
@ -1,38 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Creational\Builder;
|
||||
|
||||
use DesignPatterns\Creational\Builder\Parts\Vehicle;
|
||||
|
||||
class BikeBuilder implements BuilderInterface
|
||||
{
|
||||
/**
|
||||
* @var Parts\Bike
|
||||
*/
|
||||
private $bike;
|
||||
|
||||
public function addDoors()
|
||||
{
|
||||
}
|
||||
|
||||
public function addEngine()
|
||||
{
|
||||
$this->bike->setPart('engine', new Parts\Engine());
|
||||
}
|
||||
|
||||
public function addWheel()
|
||||
{
|
||||
$this->bike->setPart('forwardWheel', new Parts\Wheel());
|
||||
$this->bike->setPart('rearWheel', new Parts\Wheel());
|
||||
}
|
||||
|
||||
public function createVehicle()
|
||||
{
|
||||
$this->bike = new Parts\Bike();
|
||||
}
|
||||
|
||||
public function getVehicle(): Vehicle
|
||||
{
|
||||
return $this->bike;
|
||||
}
|
||||
}
|
@ -15,6 +15,7 @@ class CarBuilder implements BuilderInterface
|
||||
{
|
||||
$this->car->setPart('rightDoor', new Parts\Door());
|
||||
$this->car->setPart('leftDoor', new Parts\Door());
|
||||
$this->car->setPart('trunkLid', new Parts\Door());
|
||||
}
|
||||
|
||||
public function addEngine()
|
||||
|
@ -2,6 +2,6 @@
|
||||
|
||||
namespace DesignPatterns\Creational\Builder\Parts;
|
||||
|
||||
class Bike extends Vehicle
|
||||
class Truck extends Vehicle
|
||||
{
|
||||
}
|
@ -44,9 +44,9 @@ BuilderInterface.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
BikeBuilder.php
|
||||
TruckBuilder.php
|
||||
|
||||
.. literalinclude:: BikeBuilder.php
|
||||
.. literalinclude:: TruckBuilder.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
@ -62,9 +62,9 @@ Parts/Vehicle.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Parts/Bike.php
|
||||
Parts/Truck.php
|
||||
|
||||
.. literalinclude:: Parts/Bike.php
|
||||
.. literalinclude:: Parts/Truck.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
namespace DesignPatterns\Creational\Builder\Tests;
|
||||
|
||||
use DesignPatterns\Creational\Builder\BikeBuilder;
|
||||
use DesignPatterns\Creational\Builder\TruckBuilder;
|
||||
use DesignPatterns\Creational\Builder\CarBuilder;
|
||||
use DesignPatterns\Creational\Builder\Director;
|
||||
|
||||
@ -10,10 +10,10 @@ class DirectorTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testCanBuildBike()
|
||||
{
|
||||
$bikeBuilder = new BikeBuilder();
|
||||
$bikeBuilder = new TruckBuilder();
|
||||
$newVehicle = (new Director())->build($bikeBuilder);
|
||||
|
||||
$this->assertInstanceOf('DesignPatterns\Creational\Builder\Parts\Bike', $newVehicle);
|
||||
$this->assertInstanceOf('DesignPatterns\Creational\Builder\Parts\Truck', $newVehicle);
|
||||
}
|
||||
|
||||
public function testCanBuildCar()
|
||||
|
44
Creational/Builder/TruckBuilder.php
Normal file
44
Creational/Builder/TruckBuilder.php
Normal file
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Creational\Builder;
|
||||
|
||||
use DesignPatterns\Creational\Builder\Parts\Vehicle;
|
||||
|
||||
class TruckBuilder implements BuilderInterface
|
||||
{
|
||||
/**
|
||||
* @var Parts\Truck
|
||||
*/
|
||||
private $truck;
|
||||
|
||||
public function addDoors()
|
||||
{
|
||||
$this->truck->setPart('rightDoor', new Parts\Door());
|
||||
$this->truck->setPart('leftDoor', new Parts\Door());
|
||||
}
|
||||
|
||||
public function addEngine()
|
||||
{
|
||||
$this->truck->setPart('truckEngine', new Parts\Engine());
|
||||
}
|
||||
|
||||
public function addWheel()
|
||||
{
|
||||
$this->truck->setPart('wheel1', new Parts\Wheel());
|
||||
$this->truck->setPart('wheel2', new Parts\Wheel());
|
||||
$this->truck->setPart('wheel3', new Parts\Wheel());
|
||||
$this->truck->setPart('wheel4', new Parts\Wheel());
|
||||
$this->truck->setPart('wheel5', new Parts\Wheel());
|
||||
$this->truck->setPart('wheel6', new Parts\Wheel());
|
||||
}
|
||||
|
||||
public function createVehicle()
|
||||
{
|
||||
$this->truck = new Parts\Truck();
|
||||
}
|
||||
|
||||
public function getVehicle(): Vehicle
|
||||
{
|
||||
return $this->truck;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user