|
| 1 | +using System; |
| 2 | + |
| 3 | +namespace DecoratorPattern |
| 4 | +{ |
| 5 | +interface ICoffee |
| 6 | +{ |
| 7 | + int GetCost(); |
| 8 | + string GetDescription(); |
| 9 | +} |
| 10 | + |
| 11 | +class SimpleCoffee : ICoffee |
| 12 | +{ |
| 13 | + public int GetCost() |
| 14 | + { |
| 15 | + return 5; |
| 16 | + } |
| 17 | + |
| 18 | + public string GetDescription() |
| 19 | + { |
| 20 | + return "Simple Coffee"; |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +class MilkCoffee : ICoffee |
| 25 | +{ |
| 26 | + private readonly ICoffee mCoffee; |
| 27 | + |
| 28 | + public MilkCoffee(ICoffee coffee) |
| 29 | + { |
| 30 | + mCoffee = coffee ?? throw new ArgumentNullException("coffee", "coffee should not be null"); |
| 31 | + } |
| 32 | + public int GetCost() |
| 33 | + { |
| 34 | + return mCoffee.GetCost() + 1; |
| 35 | + } |
| 36 | + |
| 37 | + public string GetDescription() |
| 38 | + { |
| 39 | + return String.Concat(mCoffee.GetDescription(), ", milk"); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +class WhipCoffee : ICoffee |
| 44 | +{ |
| 45 | + private readonly ICoffee mCoffee; |
| 46 | + |
| 47 | + public WhipCoffee(ICoffee coffee) |
| 48 | + { |
| 49 | + mCoffee = coffee ?? throw new ArgumentNullException("coffee", "coffee should not be null"); |
| 50 | + } |
| 51 | + public int GetCost() |
| 52 | + { |
| 53 | + return mCoffee.GetCost() + 1; |
| 54 | + } |
| 55 | + |
| 56 | + public string GetDescription() |
| 57 | + { |
| 58 | + return String.Concat(mCoffee.GetDescription(), ", whip"); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +class VanillaCoffee : ICoffee |
| 63 | +{ |
| 64 | + private readonly ICoffee mCoffee; |
| 65 | + |
| 66 | + public VanillaCoffee(ICoffee coffee) |
| 67 | + { |
| 68 | + mCoffee = coffee ?? throw new ArgumentNullException("coffee", "coffee should not be null"); |
| 69 | + } |
| 70 | + public int GetCost() |
| 71 | + { |
| 72 | + return mCoffee.GetCost() + 1; |
| 73 | + } |
| 74 | + |
| 75 | + public string GetDescription() |
| 76 | + { |
| 77 | + return String.Concat(mCoffee.GetDescription(), ", vanilla"); |
| 78 | + } |
| 79 | +} |
| 80 | + class Program |
| 81 | + { |
| 82 | + static void Main(string[] args) |
| 83 | + { |
| 84 | + var myCoffee = new SimpleCoffee(); |
| 85 | + Console.WriteLine("{0:c}",myCoffee.GetCost()); // $ 5.00 |
| 86 | + Console.WriteLine("{0}", myCoffee.GetDescription()); // Simple Coffee |
| 87 | + |
| 88 | + var milkCoffee = new MilkCoffee(myCoffee); |
| 89 | + Console.WriteLine("{0:c}", milkCoffee.GetCost()); // $ 6.00 |
| 90 | + Console.WriteLine("{0}", milkCoffee.GetDescription()); // Simple Coffee, milk |
| 91 | + |
| 92 | + var whipCoffee = new WhipCoffee(milkCoffee); |
| 93 | + Console.WriteLine("{0:c}", whipCoffee.GetCost()); // $ 7.00 |
| 94 | + Console.WriteLine("{0}", whipCoffee.GetDescription()); // Simple Coffee, milk, whip |
| 95 | + |
| 96 | + var vanillaCoffee = new VanillaCoffee(whipCoffee); |
| 97 | + Console.WriteLine("{0:c}", vanillaCoffee.GetCost()); // $ 8.00 |
| 98 | + Console.WriteLine("{0}", vanillaCoffee.GetDescription()); // Simple Coffee, milk, whip |
| 99 | + Console.ReadLine(); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments