複製程式碼程式碼如下:
/**
* 職責鏈模式
*
* 為解除請求的發送者和接收者之間的耦合,而使用多個物件都用機會處理這個請求,將這些物件連成一條鏈,並沿著這條鏈傳遞該請求,直到有一個物件處理它
*
*/
抽象類別Handler
/***/
抽象類別Handler
{
受保護的$_handler = null;
public function setSuccessor($handler)
{
$this->_handler = $handler;
}
抽象函數($request);
}
class ConcreteHandlerZero 擴充處理程序
{
public function handleRequest($request)
{
if($request == 0)
{
echo "0< ;br/>”;
} else {
$this->_handler->handleRequest($request);
}
}
}
class ConcreteHandlerOdd extends Handler
{
public function handleRequest($request)
{
if($request % 2)
{
echo $request." 很奇怪
" ;
} else {
$this->_handler->handleRequest($request);
}
}
}
class ConcreteHandlerEven extends Handler
}
class ConcreteHandlerEven extends Handler
}
class ConcreteHandlerEven extends Handler
}
class ConcreteHandlerEven extends Handler
}
類function handleRequest($request)
{
if(!($request % 2))
{
echo $request." 是偶數
";
} else {
$this->_handler->handleRequest($request);
}
}
}
//實例一下
$objZeroHander = new ConcreteHandlerZero(); = new ConcreteHandlerEven();
$objOddHander = new ConcreteHandlerOdd();
$objEvenHander-> 2,3,4,5,0) as $row)
{$objZeroHander->handleRequest($row);
} 以上就介紹了責任php設計模式Chain Of Responsibility職責鏈模式,包括了責任方面的內容,希望對PHP教程有興趣的朋友有所幫助。