php设计模式 Decorator(装饰模式)
2011-06-26 00:00
2021 查看
<?php /** * 装饰模式 * * 动态的给一个对象添加一些额外的职责,就扩展功能而言比生成子类方式更为灵活 */ 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("一定要学好装饰模式<br/>"); // --- 以下是使用装饰模式 ---- 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("一定要学好装饰模式!<br/>");
相关文章推荐
- 学习php设计模式 php实现装饰器模式(decorator)
- [php]php设计模式 Decorator(装饰模式)
- 学习php设计模式 php实现装饰器模式(decorator)
- 深入浅出设计模式(九):12.装饰模式(Decorator) 13.桥模式(Bridge)14.策略模式(Strategy)
- .NET设计模式(10):装饰模式(Decorator Pattern)(转)
- 设计模式实践(四)装饰模式(decorator)
- JAVA设计模式(11):结构型-装饰模式(Decorator)
- java设计模式之装饰模式Decorator
- 乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)
- PHP设计模式之装饰器模式
- 设计模式学习笔记(三)装饰模式(Decorator)
- 设计模式学习6 -- Decorator(装饰)模式
- C++设计模式<六>:Decorator装饰模式
- C++设计模式之九:Decorator(装饰)
- 设计模式中结构型模式(四)装饰模式(Decorator)
- Java经典设计模式-结构型模式-装饰模式(Decorator)
- java23种设计模式之装饰模式(Decorator)
- 23设计模式之装饰模式(Decorator)
- 设计模式---装饰模式(Decorator)