mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-19 05:01:15 +02:00
start a restructure
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible;
|
||||
|
||||
use DesignPatterns\Behavioral\ChainOfResponsibilities\Handler;
|
||||
use DesignPatterns\Behavioral\ChainOfResponsibilities\Request;
|
||||
|
||||
/**
|
||||
* Class FastStorage
|
||||
*/
|
||||
class FastStorage extends Handler
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $data = array();
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible;
|
||||
|
||||
use DesignPatterns\Behavioral\ChainOfResponsibilities\Handler;
|
||||
use DesignPatterns\Behavioral\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
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $data = array();
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user