一、例子
在软件开发中,我们往往会想要给某一类对象增加不同的功能。比如要给汽车增加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)的更多相关文章

  1. 浅谈设计模式--装饰者模式(Decorator Pattern)

    挖了设计模式这个坑,得继续填上.继续设计模式之路.这次讨论的模式,是 装饰者模式(Decorator Pattern) 装饰者模式,有时也叫包装者(Wrapper),主要用于静态或动态地为一个特定的对 ...

  2. 设计模式 - 装饰者模式(Decorator Pattern) Java的IO类 用法

    装饰者模式(Decorator Pattern) Java的IO类 用法 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26716 ...

  3. 设计模式 - 装饰者模式(Decorator Pattern) 具体解释

    装饰者模式(Decorator Pattern) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26707033 装饰者 ...

  4. 设计模式学习--装饰者模式(Decorator Pattern)

    概念: 装饰者模式(Decorator Pattern): 动态地将功能添加到对象,相比生成子类更灵活,更富有弹性. 解决方案: 装饰者模式的重点是对象的类型,装饰者对象必须有着相同的接口,也也就是有 ...

  5. 大话设计模式--装饰者模式 Decorator -- C++实现实例

    1.装饰者模式 Decorator 动态地给一个对象添加一个额外的职责, 就添加功能来说, 装饰模式比生成子类更为灵活. 每个装饰对象的实现和如何使用这个对象分离,  每个装饰对象只关心自己的功能,不 ...

  6. 23种设计模式之装饰器模式(Decorator Pattern)

    装饰器模式(Decorator Pattern) 允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装饰类,用来包 ...

  7. 设计模式(三):“花瓶+鲜花”中的装饰者模式(Decorator Pattern)

    在前两篇博客中详细的介绍了"策略模式"和“观察者模式”,今天我们就通过花瓶与鲜花的例子来类比一下“装饰模式”(Decorator Pattern).在“装饰模式”中很好的提现了开放 ...

  8. C#设计模式之装饰者模式(Decorator Pattern)

    1.概述 装饰者模式,英文名叫做Decorator Pattern.装饰模式是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能.它是通过创建一个包装对象,也就是装饰来包裹真实的对象. 2 ...

  9. Android设计模式之中的一个个样例让你彻底明确装饰者模式(Decorator Pattern)

    导读 这篇文章中我不会使用概念性文字来说明装饰者模式.由于通常概念性的问题都非常抽象.非常难懂.使得读者非常难明确究竟为什么要使用这样的设计模式.我们设计模式的诞生,肯定是前辈们在设计程序的时候遇到了 ...

随机推荐

  1. 自定义Property属性动画

    同步发表于 http://avenwu.net/customlayout/2015/04/06/custom_property_animation/ 代码获取 git clone https://gi ...

  2. Asp.net Core WebApi 支持json/xml格式的数据返回

    Asp.net core 在做webapi项目的时候,默认是只返回json格式的数据的,如果想要开启xml数据返回,需要在startup里配置如下: public void ConfigureServ ...

  3. Three levels at which any machine carrying out an Information-Processing task must be understood

    1. Computational theory What is the goal of computation, why is it appropriate, and what is the logi ...

  4. android之AlertDialog 点击其它区域自己主动消失

    遇到一个问题记录下来,在开发中使用了AlertDialog,想点击屏幕其它区域的时候让这个dialog消失,一開始不做不论什么设置,在小米手机能够正常显示,可是在三星中却有问题.后来发现少了一个属性: ...

  5. GPS accuracy in Android

    Get the estimated accuracy of this location, in meters. We define accuracy as the radius of 68% conf ...

  6. The model backing the <Database> context has changed since the database was created.

    Just found out the answer and thought of updating here. Just need to do the following. public class ...

  7. VARCHAR 详解

    varchar(20):20指的是表中的a字段能存储的最大字符个数 In contrast to CHAR, VARCHAR values are stored as a 1-byte or 2-by ...

  8. BZOJ 1093 [ZJOI2007] 最大半连通子图(强联通缩点+DP)

    题目大意 题目是图片形式的,就简要说下题意算了 一个有向图 G=(V, E) 称为半连通的(Semi-Connected),如果满足图中任意两点 u v,存在一条从 u 到 v 的路径或者从 v 到 ...

  9. VMware的使用

    1.问题的提出   现在所有的组装台式机,均以64位操作系统作为平台   而且USB接口均以USB3.0默认   windows7 64位和windows 10 64位是主流   那么建立在windo ...

  10. IOS中多版本,多设备类型支持注意事项

    IOS系统从07年出来,到现在也有6年了,每年发布一次到两次新的设备,从iPhone1,iPhone2 ... iPhone4s再到最新的iPhone5.硬件在升级的过程中CPU的架构也可能发生变化, ...