internal static void ExecuteDecorator() { //Intent // • Attach additional responsibilities to an object dynamically. // Decorators provide a flexible alternative to subclassing for extending // functionality. // • Client - specified embellishment of a core object by recursively // wrapping it. // • Wrapping a gift, putting it in a box, and wrapping the box. //Problem // You want to add behavior or state to individual objects at run-time. // Inheritance is not feasible because it is static and applies to an entire // class. Beverage beverage = new Cream(new Cream(new Espresso())); Beverage beverage2 = new Mocha(new Milk(new Americano())); Console.WriteLine(String.Format($"Beverage: {beverage.GetName()}")); Console.WriteLine(String.Format($"Cost: {beverage.GetCost()}")); Console.WriteLine(String.Format($"Beverage: {beverage2.GetName()}")); Console.WriteLine(String.Format($"Cost: {beverage2.GetCost()}")); }
public void CreamCostTest() { CoffeeMachine coffee_machine = new Coffee(); coffee_machine = new Cream(coffee_machine); int expected = 57; int actual; actual = coffee_machine.GetCost(); Assert.AreEqual(expected, actual); }
static void Main(string[] args) { { var latte = new Latte(LatteType.Double); var cinnamon = new Cinnamon(latte); var lemon = new Lemon(cinnamon, 2); var iceCubes = new IceCubes(lemon, 2, IceCubeType.Dry); var beverage = new ChocolateCrumbs(iceCubes, 2); Console.WriteLine(beverage.GetDescription() + " costs " + beverage.GetCost()); } { var beverage = new ChocolateCrumbs( // Внешний слой: шоколадная крошка new IceCubes( // | под нею - кубики льда new Lemon( // | | еще ниже лимон new Cinnamon( // | | | слоем ниже - корица new Latte(LatteType.Standart)), // | | | в самом сердце - Латте 2), // | | 2 дольки лимона 2, IceCubeType.Dry), // | 2 кубика сухого льда 2); // 2 грамма шоколадной крошки Console.WriteLine(beverage.GetDescription() + " costs " + beverage.GetCost()); } { var capuccino = new Cappuccino(CappuccinoType.Standart); var chocolateSlice = new ChocolateSlice(capuccino, 6); var liquor = new Liquor(chocolateSlice, LiquorType.Nut); var beverage = new Cream(liquor); Console.WriteLine(beverage.GetDescription() + " costs " + beverage.GetCost()); } { var milkshake = new Milkshake(MilkshakeType.Big); var chocolateSlice = new ChocolateSlice(milkshake, 4); var liquor = new Liquor(chocolateSlice, LiquorType.Chocolate); var beverage = new Cream(liquor); Console.WriteLine(beverage.GetDescription() + " costs " + beverage.GetCost()); } }