mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-27 10:10:14 +02:00
Merge branch 'builder-pattern'
This commit is contained in:
43
Builder/BikeBuilder.php
Normal file
43
Builder/BikeBuilder.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* DesignPatternPHP
|
||||
*/
|
||||
|
||||
namespace DesignPatterns\Builder;
|
||||
|
||||
/**
|
||||
* BikeBuilder builds bike
|
||||
*/
|
||||
class BikeBuilder implements Builder
|
||||
{
|
||||
|
||||
protected $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()
|
||||
{
|
||||
return $this->bike;
|
||||
}
|
||||
|
||||
}
|
33
Builder/Builder.php
Normal file
33
Builder/Builder.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* DesignPatternPHP
|
||||
*/
|
||||
|
||||
namespace DesignPatterns\Builder;
|
||||
|
||||
/**
|
||||
* Builder is an interface that build parts of a complex object.
|
||||
*
|
||||
* Sometime, 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 vehicles, 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.
|
||||
*/
|
||||
interface Builder
|
||||
{
|
||||
|
||||
function createVehicle();
|
||||
|
||||
function addWheel();
|
||||
|
||||
function addEngine();
|
||||
|
||||
function addDoors();
|
||||
|
||||
function getVehicle();
|
||||
}
|
46
Builder/CarBuilder.php
Normal file
46
Builder/CarBuilder.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* DesignPatternPHP
|
||||
*/
|
||||
|
||||
namespace DesignPatterns\Builder;
|
||||
|
||||
/**
|
||||
* CarBuilder builds car
|
||||
*/
|
||||
class CarBuilder implements Builder
|
||||
{
|
||||
|
||||
protected $car;
|
||||
|
||||
public function addDoors()
|
||||
{
|
||||
$this->car->setPart('rightdoor', new Parts\Door());
|
||||
$this->car->setPart('leftDoor', new Parts\Door());
|
||||
}
|
||||
|
||||
public function addEngine()
|
||||
{
|
||||
$this->car->setPart('engine', new Parts\Engine());
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
public function createVehicle()
|
||||
{
|
||||
$this->car = new Parts\Car();
|
||||
}
|
||||
|
||||
public function getVehicle()
|
||||
{
|
||||
return $this->car;
|
||||
}
|
||||
|
||||
}
|
32
Builder/Director.php
Normal file
32
Builder/Director.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* DesignPatternPHP
|
||||
*/
|
||||
|
||||
namespace DesignPatterns\Builder;
|
||||
|
||||
/**
|
||||
* Director is part of the builder pattern. It knows the interface of the builder
|
||||
* and build a complex object with the help of the builder.
|
||||
* If you make it abstract, this becomes a "meta-factory-method" very powerfull.
|
||||
*
|
||||
* You can also inject many builders instead of one to build more complex objects
|
||||
*/
|
||||
class Director
|
||||
{
|
||||
|
||||
/**
|
||||
* The director don't know 'bout concrete part
|
||||
*/
|
||||
public function build(Builder $builder)
|
||||
{
|
||||
$builder->createVehicle();
|
||||
$builder->addDoors();
|
||||
$builder->addEngine();
|
||||
$builder->addWheel();
|
||||
|
||||
return $builder->getVehicle();
|
||||
}
|
||||
|
||||
}
|
15
Builder/Parts/Bike.php
Normal file
15
Builder/Parts/Bike.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* DesignPatternPHP
|
||||
*/
|
||||
|
||||
namespace DesignPatterns\Builder\Parts;
|
||||
|
||||
/**
|
||||
* Bike is a bike
|
||||
*/
|
||||
class Bike extends Vehicle
|
||||
{
|
||||
|
||||
}
|
15
Builder/Parts/Car.php
Normal file
15
Builder/Parts/Car.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* DesignPatternPHP
|
||||
*/
|
||||
|
||||
namespace DesignPatterns\Builder\Parts;
|
||||
|
||||
/**
|
||||
* Car is a car
|
||||
*/
|
||||
class Car extends Vehicle
|
||||
{
|
||||
|
||||
}
|
8
Builder/Parts/Door.php
Normal file
8
Builder/Parts/Door.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Builder\Parts;
|
||||
|
||||
class Door
|
||||
{
|
||||
|
||||
}
|
8
Builder/Parts/Engine.php
Normal file
8
Builder/Parts/Engine.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Builder\Parts;
|
||||
|
||||
class Engine
|
||||
{
|
||||
|
||||
}
|
22
Builder/Parts/Vehicle.php
Normal file
22
Builder/Parts/Vehicle.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* DesignPatternPHP
|
||||
*/
|
||||
|
||||
namespace DesignPatterns\Builder\Parts;
|
||||
|
||||
/**
|
||||
* Vehicle is a contract for a vehicle
|
||||
*/
|
||||
abstract class Vehicle
|
||||
{
|
||||
|
||||
protected $data;
|
||||
|
||||
public function setPart($key, $value)
|
||||
{
|
||||
$this->data[$key] = $value;
|
||||
}
|
||||
|
||||
}
|
8
Builder/Parts/Wheel.php
Normal file
8
Builder/Parts/Wheel.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Builder\Parts;
|
||||
|
||||
class Wheel
|
||||
{
|
||||
|
||||
}
|
46
Tests/Builder/DirectorTest.php
Normal file
46
Tests/Builder/DirectorTest.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* DesignPatternPHP
|
||||
*/
|
||||
|
||||
namespace DesignPatterns\Tests\Builder;
|
||||
|
||||
use DesignPatterns\Builder\Director;
|
||||
use DesignPatterns\Builder\CarBuilder;
|
||||
use DesignPatterns\Builder\BikeBuilder;
|
||||
|
||||
/**
|
||||
* 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(\DesignPatterns\Builder\Builder $builder)
|
||||
{
|
||||
$newVehicle = $this->director->build($builder);
|
||||
$this->assertInstanceOf('DesignPatterns\Builder\Parts\Vehicle', $newVehicle);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user