Skip to content

Instantly share code, notes, and snippets.

@vitaliy-evsyukov
Created January 27, 2012 11:57
Show Gist options
  • Save vitaliy-evsyukov/1688458 to your computer and use it in GitHub Desktop.
Save vitaliy-evsyukov/1688458 to your computer and use it in GitHub Desktop.
Chain-of-responsibility pattern
<pre>
<?php
abstract class Controller {
protected $_data = array();
protected $_chain = null;
public function __construct(array $data = array()) {
$this->_data = $data;
}
public function setChain(Handler $chain) {
$this->_chain = $chain;
}
public function getChain() {
return $this->_chain;
}
abstract public function runStrategy();
}
class C extends Controller {
public function runStrategy() {
$cs = array();
for ($i = 0; $i < 2; $i++) {
$c1 = new CA(array('word1'.$i, 'word2'.$i, 'word3'.$i, 'word4'.$i));
$c2 = new CB(array('Russia'.$i, 'USA'.$i, 'China'.$i, 'France'.$i));
$c3 = new CC(array('hi'.$i, 'bye'.$i, '9000'.$i, '!-^-!'.$i));
$cs['s'][] = $c1;
$cs['m'][] = $c2;
$cs['f'][] = $c3;
}
foreach ($cs as $k => $vs) {
foreach ($vs as $v) {
$cs['r'][] = $v;
}
}
$cs = $cs['r'];
$start = $this->getChain()->getNext();;
foreach ($cs as $c) {
$start = new Handler($start);
$start->setController($c);
}
$this->getChain()->setNext($start);
print_r($this->getChain());
}
}
class D extends Controller {
public function runStrategy() {
echo 'I am just D!<br />';
printf('My data is: %s', print_r($this->_data, true));
}
}
class CA extends Controller {
public function runStrategy() {
echo '<table border="1">';
foreach ($this->_data as $item) {
echo "<tr><td>{$item}</td></tr>";
}
echo '</table>';
}
}
class CB extends Controller {
public function runStrategy() {
foreach ($this->_data as $item) {
echo "{$item}<br />";
}
}
}
class CC extends Controller {
public function runStrategy() {
foreach ($this->_data as $item) {
echo "----{$item}<br />";
}
}
}
class Handler
{
protected $_next = null;
protected $_controller;
public function getLimit()
{
return $this->limit;
}
public function __construct(Handler $handler = null)
{
$this->setNext($handler);
}
public function setNext(Handler $handler = null) {
$this->_next = $handler;
}
public function getNext() {
return $this->_next;
}
public function getController() {
return $this->_controller;
}
public function setController(Controller $controller) {
$this->_controller = $controller;
}
public function handleRequest($state = 0)
{
if ($state === 0)
{echo 'START FK<BR />';}
$this->_controller->runStrategy();
$state++;
if ($this->_next) {
$this->_next->handleRequest($state);
}
if ($state === 1) {echo 'STOP FK<BR />';}
}
}
$cMain = new C();
$cAdditional = new D();
$cAdditional2 = new D(array('Second D'));
$start = new Handler();
$start->setController($cAdditional2);
$start = new Handler($start);
$start->setController($cAdditional);
$start = new Handler($start);
$start->setController($cMain);
$start->getController()->setChain($start);
echo '<br />';
$start->handleRequest();
?>
</pre>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment