博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
设计模式学习笔记--装饰模式
阅读量:6909 次
发布时间:2019-06-27

本文共 4075 字,大约阅读时间需要 13 分钟。

1 using System; 2  3 namespace Decorator 4 { 5     ///   6     /// 作者:bzyzhang 7     /// 时间:2016/5/21 22:56:57  8     /// 博客地址:http://www.cnblogs.com/bzyzhang/ 9     /// Beverage说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 10     ///  11     public abstract class Beverage12     {13         protected string description = "Unknown Beverage";14 15         public virtual string GetDescription()16         {17             return description;18         }19 20         public abstract double Cost();21     }22 }
View Code
1 using System; 2  3 namespace Decorator 4 { 5     ///   6     /// 作者:bzyzhang 7     /// 时间:2016/5/21 23:02:25  8     /// 博客地址:http://www.cnblogs.com/bzyzhang/ 9     /// Espresso说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 10     ///  11     public class Espresso:Beverage12     {13         public Espresso()14         {15             description = "Espresso";16         }17 18         public override double Cost()19         {20             return 1.99;21         }22     }23 }
View Code
1 using System; 2  3 namespace Decorator 4 { 5     ///   6     /// 作者:bzyzhang 7     /// 时间:2016/5/21 23:00:21  8     /// 博客地址:http://www.cnblogs.com/bzyzhang/ 9     /// CondimentDecorator说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 10     ///  11     public abstract class CondimentDecorator:Beverage12     {13         private Beverage beverage;14 15         public CondimentDecorator(Beverage beverage)16         {17             this.beverage = beverage;18         }19 20         public override string GetDescription()21         {22             return beverage.GetDescription();23         }24 25         public override double Cost()26         {27             return beverage.Cost();28         }29     }30 }
View Code
1 using System; 2  3 namespace Decorator 4 { 5     ///   6     /// 作者:bzyzhang 7     /// 时间:2016/5/21 23:07:18  8     /// 博客地址:http://www.cnblogs.com/bzyzhang/ 9     /// Mocha说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 10     ///  11     public class Mocha : CondimentDecorator12     {13         public Mocha(Beverage beverage)14             : base(beverage)15         { }16 17         public override string GetDescription()18         {19             return base.GetDescription() + "+ Mocha";20         }21 22         public override double Cost()23         {24             return base.Cost() + 0.20;25         }26     }27 }
View Code
1 using System; 2  3 namespace Decorator 4 { 5     ///   6     /// 作者:bzyzhang 7     /// 时间:2016/5/21 23:10:42  8     /// 博客地址:http://www.cnblogs.com/bzyzhang/ 9     /// Soy说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 10     ///  11     public class Soy : CondimentDecorator12     {13         public Soy(Beverage beverage)14             : base(beverage)15         { }16 17         public override string GetDescription()18         {19             return base.GetDescription() + "+ Soy";20         }21 22         public override double Cost()23         {24             return base.Cost() + 0.30;25         }26     }27 }
View Code
1 using System; 2  3 namespace Decorator 4 { 5     ///   6     /// 作者:bzyzhang 7     /// 时间:2016/5/21 23:13:05  8     /// 博客地址:http://www.cnblogs.com/bzyzhang/ 9     /// Whip说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 10     ///  11     public class Whip : CondimentDecorator12     {13         public Whip(Beverage beverage)14             : base(beverage)15         { }16 17         public override string GetDescription()18         {19             return base.GetDescription() + "+ Whip";20         }21 22         public override double Cost()23         {        24             return base.Cost() + 0.60;25         }26     }27 }
View Code
1 using System; 2  3 namespace Decorator 4 { 5     class Program 6     { 7         static void Main(string[] args) 8         { 9             Espresso beverage1 = new Espresso();10             Mocha mocha = new Mocha(beverage1);11             Soy soy = new Soy(mocha);12             Whip whip = new Whip(soy);13 14             Console.WriteLine(whip.GetDescription()+"  "+whip.Cost());15         }16     }17 }
View Code

 

转载于:https://www.cnblogs.com/bzyzhang/p/5516173.html

你可能感兴趣的文章
一起谈.NET技术,.Net4.0 Parallel编程(四)Task 上
查看>>
自定义Status Bar的基本方法
查看>>
react动画难写?试试react版transformjs
查看>>
Chrome(12)中使用getComputedStyle获取透明度(opacity)返回字符串不同于其它浏览器...
查看>>
【汉字乱码】IE下GET形式传递汉字。
查看>>
SmartImageView
查看>>
《FineUI秘密花园》在线阅读与完整PDF版
查看>>
android 混淆相关 proguard
查看>>
net.sf.json.JSONException: There is a cycle in the hierarchy!错误解决方案
查看>>
android TDD平台插入双卡时,查看允许返回发送报告的选项,去掉勾选,不起作用...
查看>>
2013年8月第2个周结
查看>>
(转)C的代码是如何变成程序的
查看>>
Udp SocketAsyncEventArgs SocketAsyncDataHandler
查看>>
音频处理平台
查看>>
jQuery(function(){})与(function(){})(jQuery)的区别
查看>>
android widget 开发实例 : 桌面便签程序的实现具体解释和源代码 (上)
查看>>
为什么需要在TypedArray后调用recycle
查看>>
安装windows7、windows8.1提示无法创建新的分区
查看>>
SpringAOP
查看>>
Java_动态重新加载Class机制
查看>>