装饰模式,PHP设计模式之责任链模式的深入解析_php技巧_脚本之家
分类:高并发

复制代码 代码如下:

复制代码 代码如下:

复制代码 代码如下:

责任链模式,其目的是组织一个对象链处理一个如方法调用的请求。当ConcreteHandler不知道如何满足来自Client的请求时,或它的目的不是这个时,它会委派给链中的下一个Handler来处理。这个设计模式通常和复合模式一起使用,其中有些叶子或容器对象默认委派操作给它们的父对象。另一个例子是,本地化通常是使用责任链处理的,当德语翻译适配器没有为翻译关键词找到合适的结果时,就返回到英语适配器或干脆直接显示关键词本身。耦合减少到最低限度:Client类不知道由哪个具体的类来处理请求;在创建对象图时配置了链;ConcreteHandlers不知道哪个对象是它们的继承者。行为在对象之间分配是成功的,链中最近的对象有优先权和责任满足请求。参与者:◆Client提交一个请求;◆Handler抽象:接收一个请求,以某种方式满足它;◆ConcreteHandlers:接收一个请求,设法满足它,如果不成功就委派给下一个处理程序。下面的代码实现了一个最着名的责任链示例:多级缓存。复制代码 代码如下:/** * The Handler abstraction. Objects that want to be a part of the * ChainOfResponsibility must implement this interface directly or via * inheritance from an AbstractHandler. */ interface KeyValueStore { /** * Obtain a value. * @param string $key * @return mixed */ public function get; } /** * Basic no-op implementation which ConcreteHandlers not interested in * caching or in interfering with the retrieval inherit from. */ abstract class AbstractKeyValueStore implements KeyValueStore { protected $_nextHandler; public function get { return $this->_nextHandler->get; } } /** * Ideally the last ConcreteHandler in the chain. At least, if inserted in * a Chain it will be the last node to be called. */ class SlowStore implements KeyValueStore { /** * This could be a somewhat slow store: a database or a flat file. */ protected $_values; public function __construct(array $values = array { $this->_values = $values; } public function get { return $this->_values[$key]; } } /** * A ConcreteHandler that handles the request for a key by looking for it in * its own cache. Forwards to the next handler in case of cache miss. */ class InMemoryKeyValueStore implements KeyValueStore { protected $_nextHandler; protected $_cached = array(); public function __construct(KeyValueStore $nextHandler) { $this->_nextHandler = $nextHandler; } protected function _load { if (!isset) { $this->_cached[$key] = $this->_nextHandler->get; } } public function get { $this->_load; return $this->_cached[$key]; } } /** * A ConcreteHandler that delegates the request without trying to * understand it at all. It may be easier to use in the user interface * because it can specialize itself by defining methods that generates * html, or by addressing similar user interface concerns. * Some Clients see this object only as an instance of KeyValueStore * and do not care how it satisfy their requests, while other ones * may use it in its entirety (similar to a class-based adapter). * No client knows that a chain of Handlers exists. */ class FrontEnd extends AbstractKeyValueStore { public function __construct(KeyValueStore $nextHandler) { $this->_nextHandler = $nextHandler; } public function getEscaped { return htmlentities, ENT_NOQUOTES, 'UTF-8'); } } // Client code $store = new SlowStore(array('pd' => 'Philip K. Dick', 'ia' => 'Isaac Asimov', 'ac' => 'Arthur C. Clarke', 'hh' => 'Helmut Heißenbüttel')); // in development, we skip cache and pass $store directly to FrontEnd $cache = new InMemoryKeyValueStore; $frontEnd = new FrontEnd; echo $frontEnd->get, "n"; echo $frontEnd->getEscaped, "n"; 关于PHP责任链设计模式的一些实现说明:◆责任链可能已经存在于对象图中,和复合模式的例子一样;◆此外,Handler抽象可能存在,也可能不存在,最好的选择是一个分开的Handler接口只可以执行handleRequest()操作,不要强制一个链只在一个层次中,因为后面的已经存在了;◆也可能引入一个抽象类,但由于请求处理是一个正交关注,因此具体的类可能已经继承了其它类;◆通过constructor 或setter,Handler被注入到Client或前一个Handler; ◆请求对象通常是一个ValueObject,也可能被实现为一个Flyweight,在PHP中,它可能是一个标量类型,如string,注意在某些语言中,一个string就是一个不变的ValueObject。简单的总结责任链模式,可以归纳为:用一系列类试图处理一个请求request,这些类之间是一个松散的耦合,唯一共同点是在他们之间传递request. 也就是说,来了一个请求,A类先处理,如果没有处理,就传递到B类处理,如果没有处理,就传递到C类处理,就这样象一个链条一样传递下去。

/**
* 职责链模式
*
* 为解除请求的发送者和接收者之间的耦合,而使用多个对象都用机会处理这个请求,将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它
*
*/
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);
}
}
}
// 实例一下
$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);
}

_handler = $handler; } abstract function handleRequest; } class ConcreteHandlerZero extends Handler { public function handleRequest { if { echo "0
"; } else { $this->_handler->handleRequest; } } } class ConcreteHandlerOdd extends Handler { public function handleRequest { if { echo $request." is odd
"; } else { $this->_handler->handleRequest; } } } class ConcreteHandlerEven extends Handler { public function handleRequest { if { echo $request." is even
"; } else { $this->_handler->handleRequest; } } } // 实例一下 $objZeroHander = new ConcreteHandlerZero(); $objEvenHander = new ConcreteHandlerEven(); $objOddHander = new ConcreteHandlerOdd(); $objZeroHander->setSuccessor; $objEvenHander->setSuccessor; foreach as $row) { $objZeroHander->handleRequest; }

/**
* 装饰模式
*
* 动态的给一个对象添加一些额外的职责,就扩展功能而言比生成子类方式更为灵活
*/
header("Content-type:text/html;charset=utf-8");
abstract class MessageBoardHandler
{
public function __construct(){}
abstract public function filter($msg);
}

class MessageBoard extends MessageBoardHandler
{
public function filter($msg)
{
return "处理留言板上的内容|".$msg;
}
}

$obj = new MessageBoard();
echo $obj->filter("一定要学好装饰模式
");

// --- 以下是使用装饰模式 ----
class MessageBoardDecorator extends MessageBoardHandler
{
private $_handler = null;

public function __construct($handler)
{
parent::__construct();
$this->_handler = $handler;
}

public function filter($msg)
{
return $this->_handler->filter($msg);
}
}

// 过滤html
class HtmlFilter extends MessageBoardDecorator
{
public function __construct($handler)
{
parent::__construct($handler);
}

public function filter($msg)
{
return "过滤掉HTML标签|".parent::filter($msg);; // 过滤掉HTML标签的处理 这时只是加个文字 没有进行处理
}
}

// 过滤敏感词
class SensitiveFilter extends MessageBoardDecorator
{
public function __construct($handler)
{
parent::__construct($handler);
}

public function filter($msg)
{
return "过滤掉敏感词|".parent::filter($msg); // 过滤掉敏感词的处理 这时只是加个文字 没有进行处理
}
}

$obj = new HtmlFilter(new SensitiveFilter(new MessageBoard()));
echo $obj->filter("一定要学好装饰模式!
");

本文由10bet手机官网发布于高并发,转载请注明出处:装饰模式,PHP设计模式之责任链模式的深入解析_php技巧_脚本之家

上一篇:没有了 下一篇:没有了
猜你喜欢
热门排行
精彩图文