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

最新文章

  1. BigInteger类
  2. C++ __int64用法(转)
  3. ios 添加到cell 上的button点击无效!扩大button的点击区域(黑魔法)
  4. win7如何完全写在iis
  5. Visual studio 能否定位打开文件在项目中的位置
  6. boost锁的使用
  7. ABAP 内表数据 与 Json串 相互转换
  8. javascript改变style样式和css样式
  9. mui返回上个页面并刷新数据
  10. 每天一个linux命令:chown
  11. 1)selenium+ java集成,待深度项目流程应用
  12. linux 下修改键盘映射
  13. iOS 10 之 网络权限带来的坑
  14. 用python与文件进行交互
  15. 00.嵌入式Linux开发环境搭建
  16. GitHub设置使用SSH Key,用TortoiseGit进行Clone仓库
  17. Educational Codeforces Round 25 E. Minimal Labels&&hdu1258
  18. 621. Task Scheduler CPU任务间隔分配器
  19. Android异步处理之AsyncTaskLoader简单使用
  20. codeblocks中给GCC编译器加参数

热门文章

  1. javascript - 简单实现一个图片延迟加载的jQuery插件
  2. 1.C#中几个简单的内置Attribute
  3. CYQ学习教程
  4. android 九宫加密记事本
  5. Mac系统下,在android studio中使用Github版本管理
  6. UIRefreshControl的使用
  7. PL/SQL Developer去掉启动时自动弹出的Logon弹出框方法
  8. STAF自动化测试框架
  9. iOS开发-pod install 出错 The dependency `AFNetworking (~> 2.6.0)` is not used in any concrete target.
  10. Outer Join Query Over Dblink Can Fail With ORA-904 (Doc ID 730256.1)