1. using System;
  2.  
  3. namespace ConsoleApplication7
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. // 我买了个苹果手机
  10. Phone phone = new ApplePhone();
  11.  
  12. // 现在想贴膜了
  13. Decorator applePhoneWithSticker = new Sticker(phone);
  14.  
  15. // 扩展贴膜行为
  16. applePhoneWithSticker.Print();
  17. Console.WriteLine("----------------------\n");
  18.  
  19. // 现在我想有挂件了
  20. Decorator applePhoneWithAccessories = new Accessories(phone);
  21.  
  22. // 扩展手机挂件行为
  23. applePhoneWithAccessories.Print();
  24. Console.WriteLine("----------------------\n");
  25.  
  26. // 现在我同时有贴膜和手机挂件了
  27. Sticker sticker = new Sticker(phone);
  28. Accessories applePhoneWithAccessoriesAndSticker = new Accessories(sticker);
  29. applePhoneWithAccessoriesAndSticker.Print();
  30. Console.ReadLine();
  31. }
  32. }
  33.  
  34. /// <summary>
  35. /// 手机抽象类,即装饰者模式中的抽象组件类
  36. /// </summary>
  37. public abstract class Phone
  38. {
  39. public abstract void Print();
  40. }
  41.  
  42. /// <summary>
  43. /// 苹果手机,即装饰着模式中的具体组件类
  44. /// </summary>
  45. public class ApplePhone : Phone
  46. {
  47. /// <summary>
  48. /// 重写基类方法
  49. /// </summary>
  50. public override void Print()
  51. {
  52. Console.WriteLine("开始执行具体的对象——苹果手机");
  53. }
  54. }
  55.  
  56. /// <summary>
  57. /// 装饰抽象类,要让装饰完全取代抽象组件,所以必须继承自Photo
  58. /// </summary>
  59. public abstract class Decorator : Phone
  60. {
  61. private Phone phone;
  62.  
  63. public Decorator(Phone p)
  64. {
  65. this.phone = p;
  66. }
  67.  
  68. public override void Print()
  69. {
  70. if (phone != null)
  71. {
  72. phone.Print();
  73. }
  74. }
  75. }
  76.  
  77. /// <summary>
  78. /// 贴膜,即具体装饰者
  79. /// </summary>
  80. public class Sticker : Decorator
  81. {
  82. public Sticker(Phone p)
  83. : base(p)
  84. {
  85. }
  86.  
  87. public override void Print()
  88. {
  89. base.Print();
  90.  
  91. // 添加新的行为
  92. AddSticker();
  93. }
  94.  
  95. /// <summary>
  96. /// 新的行为方法
  97. /// </summary>
  98. public void AddSticker()
  99. {
  100. Console.WriteLine("现在苹果手机有贴膜了");
  101. }
  102. }
  103.  
  104. /// <summary>
  105. /// 手机挂件
  106. /// </summary>
  107. public class Accessories : Decorator
  108. {
  109. public Accessories(Phone p)
  110. : base(p)
  111. {
  112. }
  113.  
  114. public override void Print()
  115. {
  116. base.Print();
  117.  
  118. // 添加新的行为
  119. AddAccessories();
  120. }
  121.  
  122. /// <summary>
  123. /// 新的行为方法
  124. /// </summary>
  125. public void AddAccessories()
  126. {
  127. Console.WriteLine("现在苹果手机有漂亮的挂件了");
  128. }
  129. }
  130. }

9.装饰者模式(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 Pattern)

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

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

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

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

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

  8. 七个结构模式之装饰者模式(Decorator Pattern)

    定义: 使用组合的方法,动态给一个类增加一些额外的功能,避免因为使用子类继承而导致类继承结构复杂.并且可以保持和被装饰者同一个抽象接口,从而使客户端透明. 结构图: Component:抽象构件类,定 ...

  9. C#设计模式——装饰者模式(Decorator Pattern)

    一.例子在软件开发中,我们往往会想要给某一类对象增加不同的功能.比如要给汽车增加ESP.天窗或者定速巡航.如果利用继承来实现,就需要定义无数的类,Car,ESPCar,CCSCar,SunRoofCa ...

随机推荐

  1. 深入解析MySQL分区(Partition)功能

    自5.1开始对分区(Partition)有支持 = 水平分区(根据列属性按行分)= 举个简单例子:一个包含十年发票记录的表可以被分区为十个不同的分区,每个分区包含的是其中一年的记录. === 水平分区 ...

  2. WAF绕过的技巧

    研究过国内外的waf.分享一些绝技. 一些大家都了解的技巧如:/*!*/,SELECT[0x09,0x0A-0x0D,0x20,0xA0]xx FROM 不再重新提及. 以下以Mysql为例讲述这些技 ...

  3. 只能输入汉字js脚本

    <html> <head> <meta http-equiv="Content-Type" content="text/html" ...

  4. 基于.NET的大型Web站点StackOverflow架构分析(转)

    Stack Overflow网址:http://stackoverflow.com/ 当前访问量:每月9500PV(每天300多万PV) 当前Alexa排名:149 所用.NET技术:C#.Visua ...

  5. win7打开网页老是提示下载网页解决办法

    方法:我的系统是windows 7 旗舰版, 解决方法可以自己手动去修复,方法是进入命令窗口. 开始--运行--cmd--sfc.exe--sfc/scannow   修复一下!

  6. ios框架中UIResponder的职责链设计模式应用

    今天有空,就把UIResponder的职责链图上传一下 如果不理解职责链设计模式的朋友,请参考:http://www.cnblogs.com/langtianya/p/4060941.html

  7. Log4j使用教程 log4:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).

    1.Logger类 通过Logger类的静态方法Logger.getRootLogger得到RootLogger.所有其他的loggers是通过静态方法Logger.getLogger来实例化并获取的 ...

  8. [整理]Code::Blocks使用遇到的问题

    在使用其编写C小程序的过程总会遇到些问题,特整理如下: 1.无法调试 注意的是项目所在的文件路径不能包含中文. 2.头文件接口函数申明引用无效 查看头文件是否处于可编译状态,左侧项目文件列表里是文件名 ...

  9. Linux大神必备-文本编辑器

    导读 我们在 Linux 上不缺乏非常现代化的编辑软件,但是它们都是基于 GUI(图形界面)的编辑软件.正如你所了解的:Linux 真正的魅力在于命令行,当你正在用命令行工作时,你就需要一个可以在控制 ...

  10. Coursera台大机器学习课程笔记11 -- Nonlinear Transformation

    这一节讲的是如何将线性不可分的情况转为非线性可分以及转换的代价.特征转换是机器学习的重点. 最后得出重要的结论是,在做转换时,先从简单模型,再到复杂模型. 参考:http://www.cnblogs. ...