php設計模式 Chain Of Responsibility (職責鏈模式)
更新時間:2011年06月26日 10:38:23 作者:
為解除請求的發(fā)送者和接收者之間的耦合,而使用多個對象都用機會處理這個請求,將這些對象連成一條鏈,并沿著這條鏈傳遞該請求,直到有一個對象處理它
復制代碼 代碼如下:
<?php
/**
* 職責鏈模式
*
* 為解除請求的發(fā)送者和接收者之間的耦合,而使用多個對象都用機會處理這個請求,將這些對象連成一條鏈,并沿著這條鏈傳遞該請求,直到有一個對象處理它
*
*/
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<br/>";
} else {
$this->_handler->handleRequest($request);
}
}
}
class ConcreteHandlerOdd extends Handler
{
public function handleRequest($request)
{
if($request % 2)
{
echo $request." is odd<br/>";
} else {
$this->_handler->handleRequest($request);
}
}
}
class ConcreteHandlerEven extends Handler
{
public function handleRequest($request)
{
if(!($request % 2))
{
echo $request." is even<br/>";
} else {
$this->_handler->handleRequest($request);
}
}
}
// 實例一下
$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);
}
相關文章
php數組函數序列之array_pop() - 刪除數組中的最后一個元素
定義和用法array_pop() 函數刪除數組中的最后一個元素。2011-11-11php5.2的curl-bug 服務器被php進程卡死問題排查
這篇文章主要介紹了php5.2的curl-bug 服務器被php進程卡死問題排查,需要的朋友可以參考下2016-09-09php實現(xiàn)批量刪除掛馬文件及批量替換頁面內容完整實例
這篇文章主要介紹了php實現(xiàn)批量刪除掛馬文件及批量替換頁面內容的方法,涉及php文件與目錄的遍歷、查找以及字符串與數組的遍歷與替換操作相關技巧,適用于utf-8編碼環(huán)境,需要的朋友可以參考下2016-07-07