设计模式初探3——装饰者模式(Decorator Pattern)
装饰者模式:动态地将责任附加到对象上。若要扩展功能,装饰者提供了比继承更有弹性的替代方案。
适用范围:
一种情况是,可能有大量独立的扩展。为支持每一种组合将产生大量的子类。使得子类数目呈爆炸性增长。
还有一种情况可能是由于类定义被隐藏,或类定义不能用于生成子类。
#include <iostream>
#include <string>
using namespace std; class Toy {
public:
virtual string getDescription() = 0;
};
我们先生产一个玩具鸭,它还不太像一仅仅鸭子,而且不能发声:
class Duck : public Toy {
public:
string getDescription(){ return "I'm a simple Toy-Duck. "; }
};
接着是装饰类的基类,它包括Toy基类的指针。以便訪问到我们的玩具鸭:
class Decorator : public Toy {
public:
Decorator(Toy* t) : toy(t){}
protected:
Toy* toy;
};
创建形状装饰者和发声装饰者:
class ShapeDecorator : public Decorator {
public:
ShapeDecorator(Toy* t) : Decorator(t){}
string getDescription() {
return toy->getDescription.append("Now I have new shape. ");
}
};
class SoundDecorator : public Decorator {
public:
SoundDecorator(Toy* t) : Decorator(t){}
string getDescription() {
return toy->getDescription.append("Now I can quack. ");
}
};
好了,我们在main函数中測试一下:
int main()
{
Toy *toy = new Duck();
cout << toy->getDescription(); toy = new ShapeDecorator(toy);
cout << toy->getDescription(); toy = new SoundDecorator(toy);
cout << toy->getDescription();
return 0;
}
让我们再关注一条十分重要的设计原则:
设计模式初探3——装饰者模式(Decorator Pattern)的更多相关文章
- 浅谈设计模式--装饰者模式(Decorator Pattern)
挖了设计模式这个坑,得继续填上.继续设计模式之路.这次讨论的模式,是 装饰者模式(Decorator Pattern) 装饰者模式,有时也叫包装者(Wrapper),主要用于静态或动态地为一个特定的对 ...
- 设计模式学习--装饰者模式(Decorator Pattern)
概念: 装饰者模式(Decorator Pattern): 动态地将功能添加到对象,相比生成子类更灵活,更富有弹性. 解决方案: 装饰者模式的重点是对象的类型,装饰者对象必须有着相同的接口,也也就是有 ...
- 设计模式 - 装饰者模式(Decorator Pattern) Java的IO类 用法
装饰者模式(Decorator Pattern) Java的IO类 用法 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26716 ...
- 设计模式 - 装饰者模式(Decorator Pattern) 具体解释
装饰者模式(Decorator Pattern) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26707033 装饰者 ...
- 23种设计模式之装饰器模式(Decorator Pattern)
装饰器模式(Decorator Pattern) 允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装饰类,用来包 ...
- 设计模式(三):“花瓶+鲜花”中的装饰者模式(Decorator Pattern)
在前两篇博客中详细的介绍了"策略模式"和“观察者模式”,今天我们就通过花瓶与鲜花的例子来类比一下“装饰模式”(Decorator Pattern).在“装饰模式”中很好的提现了开放 ...
- C#设计模式之装饰者模式(Decorator Pattern)
1.概述 装饰者模式,英文名叫做Decorator Pattern.装饰模式是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能.它是通过创建一个包装对象,也就是装饰来包裹真实的对象. 2 ...
- Android设计模式之中的一个个样例让你彻底明确装饰者模式(Decorator Pattern)
导读 这篇文章中我不会使用概念性文字来说明装饰者模式.由于通常概念性的问题都非常抽象.非常难懂.使得读者非常难明确究竟为什么要使用这样的设计模式.我们设计模式的诞生,肯定是前辈们在设计程序的时候遇到了 ...
- python 设计模式之装饰器模式 Decorator Pattern
#写在前面 已经有一个礼拜多没写博客了,因为沉醉在了<妙味>这部小说里,里面讲的是一个厨师苏秒的故事.现实中大部分人不会有她的天分.我喜欢她的性格:总是想着去解决问题,好像从来没有怨天尤人 ...
随机推荐
- 使用 Nginx 创建服务器的负载均衡
译序 Nginx 的负载均衡配置看上去很简单.以下是 Nginx 官方给的一个简单的负载均衡的例子: http { upstream myproject { server ...
- Ubuntu 安装和配置minicom
Ubuntu 安装和配置minicom 1 . 安装 Minicom 用新立得软件管理器下载minicom 2.配置Minicom shell下输入 minicom -s 打开配置界面 进入Seria ...
- Lucene.Net 2.3.1开发介绍 —— 三、索引(五)
原文:Lucene.Net 2.3.1开发介绍 -- 三.索引(五) 话接上篇,继续来说权重对排序的影响.从上面的4个测试,只能说是有个直观的理解了.“哦,是!调整权重是能影响排序了,但是好像没办法来 ...
- Android菜鸟的成长笔记(8)——Intent与Intent Filter(上)
原文:[置顶] Android菜鸟的成长笔记(8)——Intent与Intent Filter(上) Intent代表了Android应用的启动“意图”,Android应用将会根据Intent来启动指 ...
- tomcat path设置
zjtest7-app:/usr/local/apache-tomcat-7.0.55_8082/logs# netstat -nap | grep 8082 tcp 0 0 :::8082 :::* ...
- Bootstrapping (compilers) - Wikipedia, the free encyclopedia
Bootstrapping (compilers) - Wikipedia, the free encyclopedia Bootstrapping (compilers)
- timesten升级
ttIsql "DSN=ttwind;UID=cacheuser;PWD=cacheuser;OraclePWD=cacheuser;" --1.查看当前版本号 Command&g ...
- 指尖上的电商---(12)SolrAdmin中加入多核的还有一种方法
这一节中我们演示下solr中创建多核的还有一种方法. 接第10讲,首先关闭tomcatserver 1.解压solr-4.8.0后,找到solr-4.8.0以下的example目录下的multicor ...
- hdu 1530 Maximum Clique
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1530 题目分类:最大团问题 DP + DFS 代码: #include<bits/stdc++. ...
- syslog-ng 3.4.3 发布,强烈建议升级 - 开源中国社区
syslog-ng 3.4.3 发布,强烈建议升级 - 开源中国社区 syslog-ng 3.4.3 发布,强烈建议升级 oschina 发布于: 2013年08月16日 (2评) 分享到 ...