mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-05-23 23:09:43 +02:00
adding concrete handler
This commit is contained in:
parent
5b4da485d9
commit
b581aef8f2
37
ChainOfResponsibilities/Responsible/FastStorage.php
Normal file
37
ChainOfResponsibilities/Responsible/FastStorage.php
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* DesignPatternPHP
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace DesignPatterns\ChainOfResponsibilities\Responsible;
|
||||||
|
|
||||||
|
use DesignPatterns\ChainOfResponsibilities\Handler;
|
||||||
|
use DesignPatterns\ChainOfResponsibilities\Request;
|
||||||
|
|
||||||
|
class FastStorage extends Handler
|
||||||
|
{
|
||||||
|
|
||||||
|
protected $_data = array();
|
||||||
|
|
||||||
|
public function __construct($data = array())
|
||||||
|
{
|
||||||
|
$this->_data = $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function processing(Request $req)
|
||||||
|
{
|
||||||
|
if ('get' === $req->verb) {
|
||||||
|
if (array_key_exists($req->key, $this->_data)) {
|
||||||
|
// the handler IS responsible and then processes the request
|
||||||
|
$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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
44
ChainOfResponsibilities/Responsible/SlowStorage.php
Normal file
44
ChainOfResponsibilities/Responsible/SlowStorage.php
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* DesignPatternPHP
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace DesignPatterns\ChainOfResponsibilities\Responsible;
|
||||||
|
|
||||||
|
use DesignPatterns\ChainOfResponsibilities\Handler;
|
||||||
|
use DesignPatterns\ChainOfResponsibilities\Request;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is mostly the same code as FastStorage but in fact, it may greatly differs
|
||||||
|
*
|
||||||
|
* One important fact about CoR : each item in the chain MUST NOT assume its position
|
||||||
|
* in the chain. A CoR is not responsible if the request is not handled UNLESS
|
||||||
|
* you make an "ExceptionHandler" which throws execption if the request goes there.
|
||||||
|
*
|
||||||
|
* To be really extendable, each handler doesn't know if there is something after him.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class SlowStorage extends Handler
|
||||||
|
{
|
||||||
|
|
||||||
|
protected $_data = array();
|
||||||
|
|
||||||
|
public function __construct($data = array())
|
||||||
|
{
|
||||||
|
$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];
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user