static void Main(string[] args) { //下订单:2巧克力 1牛奶 的 longblack //1 longblack Drink order = new LongBlack(); Console.WriteLine("费用:\t" + order.Cost()); Console.WriteLine("描述:\t" + order.GetDescription()); //2 加牛奶 order = new Milk(order); Console.WriteLine("费用:\t" + order.Cost()); Console.WriteLine("描述:\t" + (order as Decorator).GetDescription()); //3 加巧克力 order = new Chocolate(order); Console.WriteLine("费用:\t" + order.Cost()); Console.WriteLine("描述:\t" + (order as Decorator).GetDescription()); //3 加巧克力 order = new Chocolate(order); Console.WriteLine("费用:\t" + order.Cost()); Console.WriteLine("描述:\t" + (order as Decorator).GetDescription()); Console.ReadLine(); }
static void Main(string[] args) { var teaWithSugarAndMilk = new Milk(new Sugar(new Tea())); var coffeeWithChocolate = new Chocolate(new Coffee()); Console.WriteLine(coffeeWithChocolate); Console.WriteLine(teaWithSugarAndMilk); }
static void Main(string[] args) { Beverage beverage = new Espresso(); beverage = new Milk(beverage); beverage = new Chocolate(beverage); Console.WriteLine(beverage.Description()); Console.WriteLine(beverage.Cost()); }
static void Main(string[] args) { AbstractCookie c1 = new BakedCookie(); Console.WriteLine(c1.GetDescription()); Vanilla c2 = new Vanilla(c1); Console.WriteLine(c2.GetDescription()); Chocolate c3 = new Chocolate(c2); Console.WriteLine(c3.GetDescription()); AbstractCookie c4 = new Chocolate(new Vanilla(new FrozenCookie())); Console.WriteLine(c4.GetDescription()); }
static void Main(string[] args) { // Que problema ele resolve? // Anexar responsabilidades adicionais a um objeto dinamicamente. // Decoradores fornecem uma alternativa flexível para subclasse para ampliar a // funcionalidade. //Fontes: // https://sourcemaking.com/design_patterns/decorator // https://www.youtube.com/watch?v=nk9Z1vwO3RI IceCream iceCream = new Chocolate(); iceCream = new Sprinkle(iceCream); iceCream = new Fudge(iceCream); Console.WriteLine(iceCream.Cost()); Console.ReadKey(); }
static void Main(string[] args) { IceCreamComponent my = new Chocolate(); my = new CherriesDecorator(my); IceCreamComponent your = new Vanilla(); your = new StrawberriesDecorator(your); IceCreamComponent his = new Chocolate(); his = new StrawberriesDecorator(his); his = new CherriesDecorator(his); Console.WriteLine("My icecream: {0} and it costs ${1}.", my.getDescription(), my.CalculateCost()); Console.WriteLine("Your icecream: {0} and it costs ${1}.", your.getDescription(), your.CalculateCost()); Console.WriteLine("His icecream: {0} and it costs ${1}.", his.getDescription(), his.CalculateCost()); }
static void Main(string[] args) { Console.WriteLine("Hello World!"); //单品黑咖啡 Coffee coffee = new LongBlack(); MyPrint(coffee); //黑咖啡的基础上+牛奶 coffee = new Milk(coffee);//生成装饰对象(牛奶)的同时将之前被装饰者(黑咖啡)注进去,达到累加效果 MyPrint(coffee); //依此类推 //可以继续添加装饰者 coffee = new Chocolate(coffee); MyPrint(coffee); Console.ReadLine(); }
public static void Main() { var coffee = new Coffee("Filtered"); coffee.Prepare(); Console.WriteLine("Making coffee with Milk and Chocolate"); var customizableDrink = new CustomizableDrink(coffee); Milk milk = new Milk(); Chocolate chocolate = new Chocolate(); customizableDrink.AddExtraIngredient(milk); customizableDrink.AddExtraIngredient(chocolate); customizableDrink.Prepare(); Console.WriteLine("Making coffee with Milk"); var customizableDrink = new CustomizableDrink(coffee); customizableDrink.AddExtraIngredient(milk); customizableDrink.Prepare(); }