mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-20 05:21:28 +02:00
start a restructure
This commit is contained in:
55
Creational/Builder/BikeBuilder.php
Normal file
55
Creational/Builder/BikeBuilder.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Builder;
|
||||
|
||||
/**
|
||||
* BikeBuilder builds bike
|
||||
*/
|
||||
class BikeBuilder implements BuilderInterface
|
||||
{
|
||||
/**
|
||||
* @var Parts\Bike
|
||||
*/
|
||||
protected $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()
|
||||
{
|
||||
return $this->bike;
|
||||
}
|
||||
}
|
34
Creational/Builder/BuilderInterface.php
Normal file
34
Creational/Builder/BuilderInterface.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Builder;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
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();
|
||||
}
|
58
Creational/Builder/CarBuilder.php
Normal file
58
Creational/Builder/CarBuilder.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Builder;
|
||||
|
||||
/**
|
||||
* CarBuilder builds car
|
||||
*/
|
||||
class CarBuilder implements BuilderInterface
|
||||
{
|
||||
/**
|
||||
* @var Parts\Car
|
||||
*/
|
||||
protected $car;
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function addDoors()
|
||||
{
|
||||
$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());
|
||||
$this->car->setPart('wheelRF', new Parts\Wheel());
|
||||
$this->car->setPart('wheelLR', new Parts\Wheel());
|
||||
$this->car->setPart('wheelRR', new Parts\Wheel());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function createVehicle()
|
||||
{
|
||||
$this->car = new Parts\Car();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Parts\Car
|
||||
*/
|
||||
public function getVehicle()
|
||||
{
|
||||
return $this->car;
|
||||
}
|
||||
}
|
30
Creational/Builder/Director.php
Normal file
30
Creational/Builder/Director.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\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.
|
||||
*
|
||||
* You can also inject many builders instead of one to build more complex objects
|
||||
*/
|
||||
class Director
|
||||
{
|
||||
|
||||
/**
|
||||
* The director don't know 'bout concrete part
|
||||
*
|
||||
* @param BuilderInterface $builder
|
||||
*
|
||||
* @return Parts\Vehicle
|
||||
*/
|
||||
public function build(BuilderInterface $builder)
|
||||
{
|
||||
$builder->createVehicle();
|
||||
$builder->addDoors();
|
||||
$builder->addEngine();
|
||||
$builder->addWheel();
|
||||
|
||||
return $builder->getVehicle();
|
||||
}
|
||||
}
|
11
Creational/Builder/Parts/Bike.php
Normal file
11
Creational/Builder/Parts/Bike.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Builder\Parts;
|
||||
|
||||
/**
|
||||
* Bike is a bike
|
||||
*/
|
||||
class Bike extends Vehicle
|
||||
{
|
||||
|
||||
}
|
11
Creational/Builder/Parts/Car.php
Normal file
11
Creational/Builder/Parts/Car.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Builder\Parts;
|
||||
|
||||
/**
|
||||
* Car is a car
|
||||
*/
|
||||
class Car extends Vehicle
|
||||
{
|
||||
|
||||
}
|
11
Creational/Builder/Parts/Door.php
Normal file
11
Creational/Builder/Parts/Door.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Builder\Parts;
|
||||
|
||||
/**
|
||||
* Class Door
|
||||
*/
|
||||
class Door
|
||||
{
|
||||
|
||||
}
|
11
Creational/Builder/Parts/Engine.php
Normal file
11
Creational/Builder/Parts/Engine.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Builder\Parts;
|
||||
|
||||
/**
|
||||
* Class Engine
|
||||
*/
|
||||
class Engine
|
||||
{
|
||||
|
||||
}
|
10
Creational/Builder/Parts/README.md
Normal file
10
Creational/Builder/Parts/README.md
Normal file
@@ -0,0 +1,10 @@
|
||||
#
|
||||
|
||||
# Purpose
|
||||
|
||||
|
||||
|
||||
# Examples
|
||||
|
||||
*
|
||||
|
23
Creational/Builder/Parts/Vehicle.php
Normal file
23
Creational/Builder/Parts/Vehicle.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Builder\Parts;
|
||||
|
||||
/**
|
||||
* VehicleInterface is a contract for a vehicle
|
||||
*/
|
||||
abstract class Vehicle
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $data;
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function setPart($key, $value)
|
||||
{
|
||||
$this->data[$key] = $value;
|
||||
}
|
||||
}
|
11
Creational/Builder/Parts/Wheel.php
Normal file
11
Creational/Builder/Parts/Wheel.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Builder\Parts;
|
||||
|
||||
/**
|
||||
* Class Wheel
|
||||
*/
|
||||
class Wheel
|
||||
{
|
||||
|
||||
}
|
15
Creational/Builder/README.md
Normal file
15
Creational/Builder/README.md
Normal file
@@ -0,0 +1,15 @@
|
||||
# Builder
|
||||
|
||||
## Purpose
|
||||
|
||||
Builder is an interface that build parts of a complex object.
|
||||
|
||||
Sometimes, if the builder has a better knowledge of what it builds, this interface could be an abstract class with default methods (aka adapter).
|
||||
|
||||
If you have a complex inheritance tree for objects, it is logical to have a complex inheritance tree for builders too.
|
||||
|
||||
Note: Builders have often a fluent interface, see the mock builder of PHPUnit for example.
|
||||
|
||||
## Examples
|
||||
|
||||
* PHPUnit: Mock Builder
|
42
Creational/Builder/Test/DirectorTest.php
Normal file
42
Creational/Builder/Test/DirectorTest.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Tests\Builder;
|
||||
|
||||
use DesignPatterns\Builder\Director;
|
||||
use DesignPatterns\Builder\CarBuilder;
|
||||
use DesignPatterns\Builder\BikeBuilder;
|
||||
use DesignPatterns\Builder\BuilderInterface;
|
||||
|
||||
/**
|
||||
* DirectorTest tests the builder pattern
|
||||
*/
|
||||
class DirectorTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
protected $director;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->director = new Director();
|
||||
}
|
||||
|
||||
public function getBuilder()
|
||||
{
|
||||
return array(
|
||||
array(new CarBuilder()),
|
||||
array(new BikeBuilder())
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Here we test the build process. Notice that the client don't know
|
||||
* anything about the contrete builder.
|
||||
*
|
||||
* @dataProvider getBuilder
|
||||
*/
|
||||
public function testBuild(BuilderInterface $builder)
|
||||
{
|
||||
$newVehicle = $this->director->build($builder);
|
||||
$this->assertInstanceOf('DesignPatterns\Builder\Parts\Vehicle', $newVehicle);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user