This commit is contained in:
Dominik Liebler
2013-09-13 14:30:24 +02:00
parent 8b82ed198d
commit 8d6d4584b1
6 changed files with 53 additions and 17 deletions

View File

@@ -16,5 +16,5 @@ interface MediaInterface
*
* @return string
*/
function render();
public function render();
}

View File

@@ -7,20 +7,31 @@ namespace DesignPatterns\Builder;
*/
class CarBuilder implements Builder
{
/**
* @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());
@@ -29,11 +40,17 @@ class CarBuilder implements Builder
$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

@@ -2,7 +2,10 @@
namespace DesignPatterns\Builder\Parts;
/**
* Class Wheel
*/
class Wheel
{
}
}

View File

@@ -13,7 +13,7 @@ namespace DesignPatterns\ChainOfResponsibilities;
abstract class Handler
{
/**
* @var null
* @var Handler
*/
private $successor = null;
@@ -22,14 +22,16 @@ abstract class Handler
*
* A prepend method could be done with the same spirit
*
* You could also send the successor in the contructor but in PHP it is a
* You could also send the successor in the constructor but in PHP it is a
* bad idea because you have to remove the type-hint of the parameter because
* the last handler has a null successor.
*
* And if you override the contructor, that Handler can no longer have a
* And if you override the constructor, that Handler can no longer have a
* successor. One solution is to provide a NullObject (see pattern).
* It is more preferable to keep the constructor "free" to inject services
* you need with the DiC of symfony2 for example.
*
* @param Handler $handler
*/
final public function append(Handler $handler)
{
@@ -46,6 +48,10 @@ abstract class Handler
* This approach by using a template method pattern ensures you that
* each subclass will not forget to call the successor. Beside, the returned
* boolean value indicates you if the request have been processed or not.
*
* @param Request $req
*
* @return bool
*/
final public function handle(Request $req)
{
@@ -63,7 +69,9 @@ abstract class Handler
/**
* Each concrete handler has to implement the processing of the request
*
*
* @param Request $req
*
* @return bool true if the request has been processed
*/
abstract protected function processing(Request $req);

View File

@@ -5,22 +5,30 @@ namespace DesignPatterns\ChainOfResponsibilities\Responsible;
use DesignPatterns\ChainOfResponsibilities\Handler;
use DesignPatterns\ChainOfResponsibilities\Request;
/**
* Class FastStorage
*/
class FastStorage extends Handler
{
/**
* @var array
*/
protected $data = array();
protected $_data = array();
/**
* @param array $data
*/
public function __construct($data = array())
{
$this->_data = $data;
$this->data = $data;
}
protected function processing(Request $req)
{
if ('get' === $req->verb) {
if (array_key_exists($req->key, $this->_data)) {
if (array_key_exists($req->key, $this->data)) {
// the handler IS responsible and then processes the request
$req->response = $this->_data[$req->key];
$req->response = $this->data[$req->key];
// instead of returning true, I could return the value but it proves
// to be a bad idea. What if the value IS "false" ?
return true;

View File

@@ -20,21 +20,21 @@ class SlowStorage extends Handler
/**
* @var array
*/
protected $_data = array();
protected $data = array();
/**
* @param array $data
*/
public function __construct($data = array())
{
$this->_data = $data;
$this->data = $data;
}
protected function processing(Request $req)
{
if ('get' === $req->verb) {
if (array_key_exists($req->key, $this->_data)) {
$req->response = $this->_data[$req->key];
if (array_key_exists($req->key, $this->data)) {
$req->response = $this->data[$req->key];
return true;
}