php装饰者模式简单应用案例分析
2019-10-23 18:05
1216 查看
本文实例讲述了php装饰者模式简单应用。分享给大家供大家参考,具体如下:
装饰模式指的是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能。它是通过创建一个包装对象,也就是装饰来包裹真实的对象。
示例:
A、B、C编辑同一篇文章。
class Article{ protected $content; public function __construct($info){ $this->content = $info; } } class editor_A extends Article{ public function __construct(Article $obj){ $this->content = $obj->content . '<br/>' . '编辑A新写的内容'; } public function decorator(){ return $this->content; } } class editor_B extends Article{ public function __construct(Article $obj){ $this->content = $obj->content . '<br/>' . '编辑B新写的内容'; } public function decorator(){ return $this->content; } } class editor_C extends Article{ public function __construct(Article $obj){ $this->content = $obj->content . '<br/>' . '编辑C新写的内容'; } public function decorator(){ return $this->content; } } $artCls = new Article('你好'); //编辑A先秀修改,然后编辑B修改,然后编辑C修改 $a = new editor_A($artCls); $b = new editor_B($a); $c = new editor_C($b); echo $c->decorator(); //编辑B先秀修改,然后编辑A修改 $b = new editor_B($artCls); $a = new editor_A($b); echo $a->decorator(); //重点是传递参数的地方,使用Article $obj传递上一个操作的对象, //来实现对同一个对象进行连续操作
运行结果:
你好
编辑A新写的内容
编辑B新写的内容
编辑C新写的内容你好
编辑B新写的内容
编辑A新写的内容
更多关于PHP相关内容感兴趣的读者可查看本站专题:《php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
您可能感兴趣的文章:
相关文章推荐
- 实现采用客户/服务器通信模式,基于TCP网络通信协议的多客户端简单应用之案例分析
- php设计模式之策略模式应用案例详解
- 案例分析工厂设计模式(简单-复杂-抽象)
- PHP CLI模式下的多进程应用分析
- PHP正则模式修正符的具体应用方式分析
- php设计模式之装饰模式应用案例详解
- 方法性能分析器--装饰者模式应用
- PHP CLI模式下的多进程应用分析
- php简单实现单态设计模式的方法分析
- 装饰者模式的简单应用
- 基于php设计模式中单例模式的应用分析
- php设计模式之单例、多例设计模式的应用分析
- php设计模式之单例、多例设计模式的应用分析
- 装饰者模式的简单案例理解
- php模式设计之观察者模式应用实例分析
- PHP数组操作简单案例分析
- PHP CLI模式下的多进程应用分析
- PHP 观察者模式深入理解与应用分析
- 简单工厂与工厂模式原理及案例分析
- phpwind代码分析之global.php简单说明(主要学习php基础知识的应用)