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 }
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 }
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 }
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 }
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 }
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 }
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 }