Copy CodeThe code is as follows:
/**
* Responsibility Chain Mode
*
* To unbind the sender and receiver of the request, the use of multiple objects takes the opportunity to process the request, linking the objects to a chain and passing the request along the chain until an object handles it
*
*/
Abstract class Handler
{
protected $_handler = null;
Public Function Setsuccessor ($handler)
{
$this->_handler = $handler;
}
Abstract function HandleRequest ($request);
}
Class Concretehandlerzero extends Handler
{
Public Function HandleRequest ($request)
{
if ($request = = 0)
{
echo "0
";
} else {
$this->_handler->handlerequest ($request);
}
}
}
Class Concretehandlerodd extends Handler
{
Public Function HandleRequest ($request)
{
if ($request% 2)
{
echo $request. "Is odd
";
} else {
$this->_handler->handlerequest ($request);
}
}
}
Class Concretehandlereven extends Handler
{
Public Function HandleRequest ($request)
{
if (! ( $request% 2))
{
echo $request. "is even
";
} else {
$this->_handler->handlerequest ($request);
}
}
}
Example
$objZeroHander = new Concretehandlerzero ();
$objEvenHander = new Concretehandlereven ();
$objOddHander = new Concretehandlerodd ();
$objZeroHander->setsuccessor ($objEvenHander);
$objEvenHander->setsuccessor ($objOddHander);
foreach (Array (2,3,4,5,0) as $row)
{
$objZeroHander->handlerequest ($row);
}
The above describes the Responsibility PHP design mode Chain of the Responsibility responsibility chain model, including the Responsibility aspects of the content, I hope to be interested in PHP tutorial friends helpful.