Responsibility PHP Design mode Chain of responsibility responsibility Chain mode

Source: Internet
Author: User
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.