Last active
December 17, 2015 02:49
-
-
Save ftdebugger/5539019 to your computer and use it in GitHub Desktop.
Example for http://dron.by/post/pattern_proektirovaniya_chain_of_responsibility__tsepochka_obyazannostey__na_php.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
abstract class AbstractHandler | |
{ | |
/** | |
* @var AbstractHandler | |
*/ | |
protected $_next; | |
/** | |
* Send request by | |
* | |
* @param mixed $message | |
*/ | |
abstract public function sendRequest($message); | |
/** | |
* @param \AbstractHandler $next | |
*/ | |
public function setNext($next) | |
{ | |
$this->_next = $next; | |
} | |
/** | |
* @return \AbstractHandler | |
*/ | |
public function getNext() | |
{ | |
return $this->_next; | |
} | |
} | |
class ConcreteHandlerA extends AbstractHandler | |
{ | |
/** | |
* @param mixed $message | |
*/ | |
public function sendRequest($message) | |
{ | |
if ($message == 1) { | |
echo __CLASS__ . "process this message"; | |
} | |
else { | |
if ($this->getNext()) { | |
$this->getNext()->sendRequest($message); | |
} | |
} | |
} | |
} | |
class ConcreteHandlerB extends AbstractHandler | |
{ | |
/** | |
* @param mixed $message | |
*/ | |
public function sendRequest($message) | |
{ | |
if ($message == 2) { | |
echo __CLASS__ . "process this message"; | |
} | |
else { | |
if ($this->getNext()) { | |
$this->getNext()->sendRequest($message); | |
} | |
} | |
} | |
} | |
$handler = new ConcreteHandlerA(); | |
$handler->setNext(new ConcreteHandlerB()); | |
$handler->sendRequest(1); | |
$handler->sendRequest(2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment