博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
设计模式 - 装饰器模式(Decorator)
阅读量:4545 次
发布时间:2019-06-08

本文共 2116 字,大约阅读时间需要 7 分钟。

简介

场景

通过继承和关联都可以给对象增加行为,区别如下:

  • 继承是静态的(无法在程序运行时动态扩展),且作用于所有子类。硬编码,高耦合。
  • 通过装饰器可以在运行时添加行为和属性到指定对象。关联关系就是在一个类中嵌入另一个类的对象,被嵌入的对象就是装饰器。可以动态决定是否调用这个内嵌对象,低耦合。

模式定义

装饰模式:动态地给指定对象增加额外职责。

装饰模式对客户透明,可以嵌套执行多次装饰,顺序不影响结果。

就增加对象功能来说,装饰模式(关联关系)比继承和实现这两种关系更为灵活。

模式特点

装饰模式包含 4 种角色:

  • Component:抽象构件
  • ConcreteComponent:具体构件
  • Decorator:抽象装饰类
  • ConcreteDecorator:具体装饰类

这里写图片描述

优缺点

优点:

  • 装饰模式可以动态扩展一个对象的功能,可以在运行时通过配置文件选择不同的装饰器,从而实现不同的行为
  • 装饰类可以排列组合,创造出很多不同行为的组合
  • 装饰类扩展时,原有代码无须改变,符合“开闭原则”

缺点:

  • 使用装饰模式进行系统设计时将产生很多小对象,每个对象仅负责一部分装饰任务
  • 多次装饰的对象,调试时寻找错误可能需要逐级排查,较为烦琐

PHP 代码示例

下面例子中,通过不同的装饰器类对标题进行不同的操作:

title = $title; $this->author = $author; } public function getTitle() {
return $this->title; }}class TitleDecorator {
protected $book; protected $title; public function __construct(Book $book) {
$this->book = $book; $this->resetTitle(); } public function resetTitle() {
$this->title = $this->book->getTitle(); } public function showTitle() {
return $this->title; }}class TitleStarDecorator extends TitleDecorator {
private $titleDecorator; public function __construct(TitleDecorator $td) {
$this->titleDecorator = $td; } public function starTitle() {
$this->titleDecorator->title = str_replace(" ","*",$this->titleDecorator->title); }}class TitleQuoteDecorator extends TitleDecorator {
private $titleDecorator; public function __construct(TitleDecorator $td) {
$this->titleDecorator = $td; } public function quoteTitle() {
$this->titleDecorator->title = "《".$this->titleDecorator->title."》"; }}$book = new Book("a good world", "Lu Xun");$td = new TitleDecorator($book);$tsd = new TitleStarDecorator($td);$tqd = new TitleQuoteDecorator($td);function write_ln($str) {
echo $str.PHP_EOL.'
';}write_ln($book->getTitle());$tsd->starTitle();write_ln($td->showTitle());$tqd->quoteTitle();write_ln($td->showTitle());$tqd->quoteTitle();write_ln($td->showTitle());

输出如下:

a good world a*good*world 《a*good*world》 《《a*good*world》》

转载于:https://www.cnblogs.com/kika/p/10851571.html

你可能感兴趣的文章
监听键盘弹出 隐藏
查看>>
iOS开发 - NSBundle, NSDevice, NSLocale
查看>>
innerHtml安全问题
查看>>
UVA 11992,。。。伪-二维线段树
查看>>
[原创]通过函数指针实现事件消息处理
查看>>
IE下JS保存图片
查看>>
293.Flip Game
查看>>
uvaLive5713 次小生成树
查看>>
mysql原生语句基础知识
查看>>
Ubuntu11搭建QT开发环境
查看>>
深度学习样本不均衡问题解决
查看>>
Servlet中Web.xml的配置详解
查看>>
RabbitMQ headers Exchange
查看>>
硬件产品测试
查看>>
nmon for linux
查看>>
H5 EventSource 实现web页面推送功能demo
查看>>
Android JNI 学习(十):String Operations Api & Other Apis
查看>>
AutoMapper
查看>>
ecshop绕过验证码暴力破解
查看>>
数组和字符串的API使用
查看>>