3.装饰者模式(Head First 设计模式复习笔记)

  1 public abstract class Beverage
  2 {
  3     public string description="just beverage without any thing";
  4 
  5     public virtual string GetDescription() {
  6         return description;
  7     }
  8 
  9     public abstract double Cost();
 10 }
 11 
 12 public abstract class CondimentDecorator:Beverage
 13 {
 14     public override string GetDescription() {
 15         return "";
 16     }
 17 }
 18 
 19 public class DarkRoast : Beverage
 20 {
 21     public DarkRoast()
 22     {
 23         description = "DarkRoast";
 24     }
 25     public override double Cost()
 26     {
 27         return 20;
 28     }
 29 }
 30 
 31 public class Decaf : Beverage
 32 {
 33     public Decaf()
 34     {
 35         description = "Decaf";
 36     }
 37     public override double Cost()
 38     {
 39         return 40;
 40     }
 41 }
 42 
 43 public class Espresso : Beverage
 44 {
 45     public Espresso()
 46     {
 47         description = "Espresso";
 48     }
 49     public override double Cost()
 50     {
 51         return 30;
 52     }
 53 }
 54 
 55 public class HouseBlend : Beverage
 56 {
 57     public  HouseBlend() {
 58         description = "HouseBlend";
 59     }
 60     public override double Cost()
 61     {
 62         return 10;
 63     }
 64 }
 65 
 66 public class Mike : CondimentDecorator
 67 {
 68     public Beverage beverage;
 69     public Mike(Beverage _beverage) {
 70         beverage = _beverage;
 71     }
 72 
 73     public override double Cost()
 74     {
 75         return beverage.Cost()+0.1;
 76     }
 77 
 78     public override string GetDescription()
 79     {
 80         return beverage.GetDescription() + ",Mike";
 81     }
 82 }
 83 
 84 public class Mocha : CondimentDecorator
 85 {
 86     public Beverage beverage;
 87     public Mocha(Beverage _beverage)
 88     {
 89         beverage = _beverage;
 90     }
 91         
 92     public override double Cost()
 93     {
 94         return beverage.Cost() + 0.01;
 95     }
 96 
 97     public override string GetDescription()
 98     {
 99         return beverage.GetDescription() + ",Mocha";
100     }
101 }
102 
103 public class Soy : CondimentDecorator
104 {
105     public Beverage beverage;
106     public Soy(Beverage _beverage)
107     {
108         beverage = _beverage;
109     }
110         
111 
112     public override double Cost()
113     {
114         return beverage.Cost() + 0.001;
115     }
116 
117     public override string GetDescription()
118     {
119         return beverage.GetDescription() + ",Soy";
120     }
121 }
122 
123 public class Whip : CondimentDecorator
124 {
125     public Beverage beverage;
126     public Whip(Beverage _beverage)
127     {
128         beverage = _beverage;
129     }
130         
131 
132     public override double Cost()
133     {
134         return beverage.Cost() + 0.0001;
135     }
136 
137     public override string GetDescription()
138     {
139         return beverage.GetDescription() + ",Whip";
140     }
141 }
142 
143 class Program
144 {
145     static void Main(string[] args)
146     {
147         Console.WriteLine("顾客:来一杯Houseblend,什么也不加!");
148         Beverage b = new HouseBlend();
149         Console.WriteLine("这是您点的{0},一共消费:{1}元", b.GetDescription(), b.Cost());
150 
151         Console.WriteLine("顾客:DarkRoast,加Mike和Mocha!");
152         Beverage b1 = new DarkRoast();
153         b1 = new Mike(b1);
154         b1 = new Mocha(b1);
155         Console.WriteLine("这是您点的{0},一共消费:{1}元", b1.GetDescription(), b1.Cost());
156 
157         Console.WriteLine("顾客:Espresso,加双份Soy!");
158         Beverage b2 = new DarkRoast();
159         b2 = new Soy(b2);
160         b2 = new Soy(b2);
161         Console.WriteLine("这是您点的{0},一共消费:{1}元", b2.GetDescription(), b2.Cost());
162 
163         Console.WriteLine("顾客:来一杯Decaf,加soy,whip,Mike,Mocha!");
164         Beverage b3 = new HouseBlend();
165         b3 = new Soy(b3);
166         b3 = new Whip(b3);
167         b3 = new Mike(b3);
168         b3 = new Mocha(b3);
169         Console.WriteLine("这是您点的{0},一共消费:{1}元", b3.GetDescription(), b3.Cost());
170         Console.ReadKey();
171     }
172 }

 

posted @ 2017-11-20 16:50  无限天  阅读(182)  评论(0编辑  收藏  举报