设计模式学习笔记--装饰模式
Posted bzyzhang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设计模式学习笔记--装饰模式相关的知识,希望对你有一定的参考价值。
1 using System; 2 3 namespace Decorator 4 { 5 /// <summary> 6 /// 作者:bzyzhang 7 /// 时间:2016/5/21 22:56:57 8 /// 博客地址:http://www.cnblogs.com/bzyzhang/ 9 /// Beverage说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 10 /// </summary> 11 public abstract class Beverage 12 { 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 }
1 using System; 2 3 namespace Decorator 4 { 5 /// <summary> 6 /// 作者:bzyzhang 7 /// 时间:2016/5/21 23:02:25 8 /// 博客地址:http://www.cnblogs.com/bzyzhang/ 9 /// Espresso说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 10 /// </summary> 11 public class Espresso:Beverage 12 { 13 public Espresso() 14 { 15 description = "Espresso"; 16 } 17 18 public override double Cost() 19 { 20 return 1.99; 21 } 22 } 23 }
1 using System; 2 3 namespace Decorator 4 { 5 /// <summary> 6 /// 作者:bzyzhang 7 /// 时间:2016/5/21 23:00:21 8 /// 博客地址:http://www.cnblogs.com/bzyzhang/ 9 /// CondimentDecorator说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 10 /// </summary> 11 public abstract class CondimentDecorator:Beverage 12 { 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 }
1 using System; 2 3 namespace Decorator 4 { 5 /// <summary> 6 /// 作者:bzyzhang 7 /// 时间:2016/5/21 23:07:18 8 /// 博客地址:http://www.cnblogs.com/bzyzhang/ 9 /// Mocha说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 10 /// </summary> 11 public class Mocha : CondimentDecorator 12 { 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 }
1 using System; 2 3 namespace Decorator 4 { 5 /// <summary> 6 /// 作者:bzyzhang 7 /// 时间:2016/5/21 23:10:42 8 /// 博客地址:http://www.cnblogs.com/bzyzhang/ 9 /// Soy说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 10 /// </summary> 11 public class Soy : CondimentDecorator 12 { 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 }
1 using System; 2 3 namespace Decorator 4 { 5 /// <summary> 6 /// 作者:bzyzhang 7 /// 时间:2016/5/21 23:13:05 8 /// 博客地址:http://www.cnblogs.com/bzyzhang/ 9 /// Whip说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 10 /// </summary> 11 public class Whip : CondimentDecorator 12 { 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 }
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 }
以上是关于设计模式学习笔记--装饰模式的主要内容,如果未能解决你的问题,请参考以下文章
小白自我提高学习设计模式笔记—装饰者模式在Android开发的小试