C#设计模式——装饰者模式(Decorator Pattern)
一、例子
在软件开发中,我们往往会想要给某一类对象增加不同的功能。比如要给汽车增加ESP、天窗或者定速巡航。如果利用继承来实现,就需要定义无数的类,Car,ESPCar,CCSCar,SunRoofCar,ESPCCSCar……很容易就导致“子类爆炸”问题。
上述“子类爆炸”问题的根源在于该解决方案利用继承来扩展功能,缺乏灵活性。那么如何能在扩展功能的同时避免“子类爆炸”呢?
二、装饰者模式
装饰者模式装饰者模式可以动态地给一个对象增加一些额外的职责。就增加功能来说,装饰者模式相比生成子类更为灵活。
装饰者模式的结构图如下

Component定义了一个接口,可以利用Decorator为实现了这个接口的对象动态地增加职责
ConcreteComponent定义了一个实现了Component接口的对象
Decorator是抽象装饰者类,继承自Component接口,并包含有一个实现了Component接口的对象
ConcreteDecorator是具体装饰者类,用于为ConcreteComponent增加职责
在以上结构中,Component和Decorator可以情况省略。
三、实现
下面用装饰者模式来解决前面的例子
首先实现Car

1 publicclass Car
2 {
3 publicvirtualvoid Description()
4 {
5 Console.WriteLine("Basic Car");
6 }
7 }

接下来实现ConcreteDecorator
ESP装饰者

1 publicclass ESPDecorator : Car
2 {
3 Car _car;
4
5 public ESPDecorator(Car car)
6 {
7 _car = car;
8 }
9
10 publicoverridevoid Description()
11 {
12 _car.Description();
13 Console.WriteLine("Add ESP");
14 }
15 }

定速巡航装饰者

1 publicclass CCSDecorator : Car
2 {
3 Car _car;
4
5 public CCSDecorator(Car car)
6 {
7 _car = car;
8 }
9
10 publicoverridevoid Description()
11 {
12 _car.Description();
13 Console.WriteLine("Add CCS");
14 }
15 }

天窗装饰者

1 publicclass SunRoofDecorator : Car
2 {
3 Car _car;
4
5 public SunRoofDecorator(Car car)
6 {
7 _car = car;
8 }
9
10 publicoverridevoid Description()
11 {
12 _car.Description();
13 Console.WriteLine("Add SunRoof");
14 }
15 }

最后看一下如何使用装饰者来给Car增加功能

1 staticvoid Main(string[] args)
2 {
3 Car car =new ESPDecorator(new CCSDecorator(new SunRoofDecorator(new Car())));
4 car.Description();
5 Console.ReadLine();
6 }

C#设计模式——装饰者模式(Decorator Pattern)的更多相关文章
- 浅谈设计模式--装饰者模式(Decorator Pattern)
挖了设计模式这个坑,得继续填上.继续设计模式之路.这次讨论的模式,是 装饰者模式(Decorator Pattern) 装饰者模式,有时也叫包装者(Wrapper),主要用于静态或动态地为一个特定的对 ...
- 设计模式 - 装饰者模式(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 装饰者 ...
- 设计模式学习--装饰者模式(Decorator Pattern)
概念: 装饰者模式(Decorator Pattern): 动态地将功能添加到对象,相比生成子类更灵活,更富有弹性. 解决方案: 装饰者模式的重点是对象的类型,装饰者对象必须有着相同的接口,也也就是有 ...
- 大话设计模式--装饰者模式 Decorator -- C++实现实例
1.装饰者模式 Decorator 动态地给一个对象添加一个额外的职责, 就添加功能来说, 装饰模式比生成子类更为灵活. 每个装饰对象的实现和如何使用这个对象分离, 每个装饰对象只关心自己的功能,不 ...
- 23种设计模式之装饰器模式(Decorator Pattern)
装饰器模式(Decorator Pattern) 允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装饰类,用来包 ...
- 设计模式(三):“花瓶+鲜花”中的装饰者模式(Decorator Pattern)
在前两篇博客中详细的介绍了"策略模式"和“观察者模式”,今天我们就通过花瓶与鲜花的例子来类比一下“装饰模式”(Decorator Pattern).在“装饰模式”中很好的提现了开放 ...
- C#设计模式之装饰者模式(Decorator Pattern)
1.概述 装饰者模式,英文名叫做Decorator Pattern.装饰模式是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能.它是通过创建一个包装对象,也就是装饰来包裹真实的对象. 2 ...
- Android设计模式之中的一个个样例让你彻底明确装饰者模式(Decorator Pattern)
导读 这篇文章中我不会使用概念性文字来说明装饰者模式.由于通常概念性的问题都非常抽象.非常难懂.使得读者非常难明确究竟为什么要使用这样的设计模式.我们设计模式的诞生,肯定是前辈们在设计程序的时候遇到了 ...
随机推荐
- vim 多行注释消除注释,多行删除
进入可视化模式: Ctrl+v 继续进入编辑模式: shift+i 注释: shift+# 注释生效: ESC 取消注释 d 删除 选中全部字符块区域,使用方向键上下右: 然后,按一下d
- 翻译--Blazing fast node.js: 10 performance tips from LinkedIn Mobile
1.避免使用同步代码: // Good: write files asynchronously fs.writeFile('message.txt', 'Hello Node', function ( ...
- WPF总结
WPF UI布局 模板总结控件可以通过ItemTemplate="{StaticResource Template}"绑定指定的模板: 数据源总结控件可以通过ItemsSource ...
- Visual Studio 新建项目报错" this template attempted to load component assembly 'NuGet.VisualStudio.Interop, ….".
"Error: this template attempted to load component assembly 'NuGet.VisualStudio.Interop, Version ...
- SecureCrt设置字符编码
SecureCrt设置字符编码,参考:http://www.2cto.com/os/201412/365535.html
- 延迟渲染 deferred Shading
流程: 1.先渲染一遍物体的位置,法线 和颜色 到三张纹理 2.在根据这三张纹理渲染一遍灯光 3.合成颜色图和灯光图 (ssao图) 看上去好像灯光不多,其实我在这里加了200个灯, 每个球代表 ...
- android EditText inputType说明
在开发的过程中,通常会用到EditText,如何让虚拟键盘来适应输入框中内容的类型,通常我们都会在xml文件中加入android:inputType="". android:inp ...
- 命令行 更新Android sdk
使用如下代理服务器: 大连东软信息学院镜像服务器地址: http://mirrors.neusoft.edu.cn 端口:80 北京化工大学镜像服务器地址: IPv4: http://ubuntu.b ...
- Codeforces Beta Round #13 C. Sequence (DP)
题目大意 给一个数列,长度不超过 5000,每次可以将其中的一个数加 1 或者减 1,问,最少需要多少次操作,才能使得这个数列单调不降 数列中每个数为 -109-109 中的一个数 做法分析 先这样考 ...
- Screen Space Subsurface Scatting(Skin Rendring)
还差通透度计算,RenderMonkey截图. 参考: http://developer.download.nvidia.com/presentations/2007/gdc/Advanced_Ski ...