start a restructure

This commit is contained in:
Antonio Spinelli
2014-03-21 18:03:44 -03:00
parent b0b0d4a1a4
commit e59d70a0ac
180 changed files with 21 additions and 16 deletions

View 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;
}
}

View 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();
}

View 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;
}
}

View 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();
}
}

View File

@@ -0,0 +1,11 @@
<?php
namespace DesignPatterns\Builder\Parts;
/**
* Bike is a bike
*/
class Bike extends Vehicle
{
}

View File

@@ -0,0 +1,11 @@
<?php
namespace DesignPatterns\Builder\Parts;
/**
* Car is a car
*/
class Car extends Vehicle
{
}

View File

@@ -0,0 +1,11 @@
<?php
namespace DesignPatterns\Builder\Parts;
/**
* Class Door
*/
class Door
{
}

View File

@@ -0,0 +1,11 @@
<?php
namespace DesignPatterns\Builder\Parts;
/**
* Class Engine
*/
class Engine
{
}

View File

@@ -0,0 +1,10 @@
#
# Purpose
# Examples
*

View 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;
}
}

View File

@@ -0,0 +1,11 @@
<?php
namespace DesignPatterns\Builder\Parts;
/**
* Class Wheel
*/
class Wheel
{
}

View 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

View 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);
}
}