/* * The Decorator Pattern * * attached additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality. * * Example: * Coffe Shop * 3 types of coffee with price: Espresso $2.5, Mocha $3, Capuccino $3, etc. * Each of which can have extras/condiments: Soy $1, Whip $0.5, Double $1.5, Decaf $1, etc. * * Problem: * 1. Class Chaos: Abstract Beverage class -> Coffee subclasses, override cost() -> more sub-classes for options * 2. Boolean properties on Beverage class -> Set options on each coffee instance -> calculate cost in the instance. Inflexible design, price change is problematic. * * Calsses should be open for extension but closed for modification. [Open-Closed Principle] (Design Principle #5) * Easily augment what we have. * * Solution: * Every new extra will encapsulate already existing i.e. new Double(new Whip(new Mocha())) so that every new option has its cost() method. * */ static void Main(string[] args) { //plain Espresso Beverage beverage = new Espresso(); Console.WriteLine($"{beverage.GetDescription()} ${beverage.Cost()}"); //Fancy Flat White Beverage beverage1 = new FlatWhite(); beverage1 = new SoyMilk(beverage1); beverage1 = new Syrup(beverage1); Console.WriteLine($"{beverage1.GetDescription()} ${beverage1.Cost()}"); //Iced Espresso Beverage beverage2 = new Espresso(); beverage2 = new Ice(beverage2); Console.WriteLine($"{beverage2.GetDescription()} ${beverage2.Cost()}"); //Capuccino with everything Beverage cap = new Capuccino(); cap = new SoyMilk(cap); cap = new Syrup(cap); cap = new Ice(cap); Console.WriteLine($"{cap.GetDescription()} ${cap.Cost()}"); Console.ReadLine(); }
static void Main(string[] args) { Beverage beverage = new Espresso(); Console.WriteLine($"{beverage.GetDescription()} {beverage.Cost()}"); Beverage beverage2 = new HouseBlend(); Console.WriteLine($"{beverage2.GetDescription()} {beverage2.Cost()}"); beverage2 = new Mocha(beverage2); beverage2 = new Mocha(beverage2); beverage2 = new Whip(beverage2); Console.WriteLine($"{beverage2.GetDescription()} {beverage2.Cost()}"); Beverage beverage3 = new HouseBlend(); Console.WriteLine($"{beverage3.GetDescription()} {beverage3.Cost()}"); beverage3 = new Mocha(beverage3); beverage3 = new Soy(beverage3); beverage3 = new Mocha(beverage3); beverage3 = new Whip(beverage3); Console.WriteLine($"{beverage3.GetDescription()} {beverage3.Cost()}"); Console.ReadLine(); }
static void Main(string[] args) { Beverage beverage = new Espresso(Size.Grande); Console.WriteLine($"{beverage.GetDescription()} ${beverage.Cost()}"); Beverage beverage2 = new DarkRoast(Size.Venti); beverage2 = new Mocha(beverage2); beverage2 = new Mocha(beverage2); beverage2 = new Whip(beverage2); beverage2 = new SteamedMilk(beverage2); Console.WriteLine($"{beverage2.GetDescription()} ${beverage2.Cost()}"); Beverage beverage3 = new HouseBlend(Size.Tall); beverage3 = new Soy(beverage3); beverage3 = new Mocha(beverage3); beverage3 = new Whip(beverage3); Console.WriteLine($"{beverage3.GetDescription()} ${beverage3.Cost()}"); Beverage beverage4 = new HouseBlend(Size.Grande); beverage4 = new Soy(beverage4); beverage4 = new Mocha(beverage4); beverage4 = new Mocha(beverage4); beverage4 = new Whip(beverage4); Console.WriteLine($"{beverage4.GetDescription()} ${beverage4.Cost()}"); Console.ReadLine(); }
static void Main(string[] args) { IDrink espresso = new Espresso(new Chocolate(new Milk())); Console.WriteLine(espresso.GetDescription()); Console.WriteLine(espresso.GetCost()); }
static void Main(string[] args) { var espresso = new Espresso(); Console.WriteLine(espresso.GetDescription() + " - $" + espresso.GetCost()); var espressoWithMilk = new Milk(new Espresso()); Console.WriteLine(espressoWithMilk.GetDescription() + " - $" + espressoWithMilk.GetCost()); Console.ReadKey(); }
static void Main(string[] args) { Beverage beverage = new Espresso(); Console.WriteLine(beverage.GetDescription() + "$" + beverage.cost().ToString()); Beverage beverage2 = new DarkRoast(); beverage2 = new Mocha(beverage2); beverage2 = new Mocha(beverage2); //用两个Mocha修饰 beverage2 = new Whip(beverage2); //在用Whip修饰。 Console.WriteLine(beverage2.GetDescription() + "$" + beverage2.cost().ToString()); Console.ReadKey(); }
static void Main(string[] args) { Beverage beverage = new Espresso(); Console.WriteLine(beverage.GetDescription() + "$" + beverage.Cost()); Beverage beverage2 = new DarkRoast(); beverage2 = new Mocah(beverage2); beverage2 = new Mocah(beverage2); beverage2 = new Whip(beverage2); Console.WriteLine(beverage2.GetDescription() + "$" + beverage2.Cost()); Console.ReadLine(); }
static void Main(string[] args) { Beverage beverage = new Espresso(); Console.WriteLine(beverage.GetDescription() + " $" + beverage.Cost()); Beverage beverage2 = new Whip(new Mocha(new Mocha(new DarkRoast()))); Console.WriteLine(beverage2.GetDescription() + " $" + beverage2.Cost()); Beverage beverage3 = new Whip(new Mocha(new Soy(new HouseBlend()))); Console.WriteLine(beverage3.GetDescription() + " $" + beverage3.Cost()); Console.ReadKey(); }
static void Main(string[] args) { var espresso = new Espresso(); Console.WriteLine(espresso.GetDescription()); Console.WriteLine(espresso.Cost()); var espressoWithChoc = new HotChocolate(espresso); Console.WriteLine(espressoWithChoc.GetDescription()); Console.WriteLine(espressoWithChoc.Cost()); var espressoWithChockAndDoubleSyrop = new MapleSyrup(new MapleSyrup(espressoWithChoc)); Console.WriteLine(espressoWithChockAndDoubleSyrop.GetDescription()); Console.WriteLine(espressoWithChockAndDoubleSyrop.Cost()); }
public static void MakeSomeCoffee() { Beverage beverage = new Espresso(); Console.WriteLine($"{beverage.GetDescription()} ${beverage.Cost()}"); Beverage darkRoast = new DarkRoast(); darkRoast = new Mocha(darkRoast); darkRoast = new Mocha(darkRoast); darkRoast = new Whip(darkRoast); Console.WriteLine($"{darkRoast.GetDescription()} ${darkRoast.Cost()}"); Beverage houseBlend = new HouseBlend(); houseBlend.SetSize(Size.VENTI); houseBlend = new Soy(houseBlend); houseBlend = new Mocha(houseBlend); houseBlend = new Whip(houseBlend); Console.WriteLine($"{houseBlend.GetDescription()} ${houseBlend.Cost()}"); }
static void Main(string[] args) { BeverageBase espresso = new Espresso(); Console.WriteLine($"{espresso.GetDescription()} ${espresso.Cost()}"); BeverageBase houseBlend = new HouseBlend(); houseBlend = new Mocha(houseBlend); houseBlend = new Mocha(houseBlend); houseBlend = new WhipCream(houseBlend); Console.WriteLine($"{houseBlend.GetDescription()} ${houseBlend.Cost()}"); BeverageBase darkRoast = new DarkRoast(); darkRoast = new SoyMilk(darkRoast); darkRoast = new Mocha(darkRoast); darkRoast = new WhipCream(darkRoast); Console.WriteLine($"{darkRoast.GetDescription()} ${darkRoast.Cost()}"); Console.ReadLine(); }
static void Main(string[] args) { Beverage beverage = new Espresso(); Console.WriteLine("Description: {0} \r\n Coast: {1}", beverage.GetDescription(), beverage.Coast()); Beverage beverage2 = new DarkRoast(); beverage2 = new Mocha(beverage2); beverage2 = new Mocha(beverage2); beverage2 = new Whip(beverage2); Console.WriteLine("Description: {0} \r\n Coast: {1}", beverage2.GetDescription(), beverage2.Coast()); Beverage beverage3 = new HouseBlend(); beverage3 = new Soy(beverage3); beverage3 = new Mocha(beverage3); beverage3 = new Whip(beverage3); Console.WriteLine("Description: {0} \r\n Coast: {1}", beverage3.GetDescription(), beverage3.Coast()); Console.ReadKey(); }
static void Main(string[] args) { Beverage beverage1 = new Espresso(); Console.WriteLine("{0} costs {1}$", beverage1.GetDescription(), beverage1.cost()); Beverage beverage2 = new DarkRoast(); beverage2 = new Milk(beverage2); beverage2 = new Mocha(beverage2); beverage2 = new Whip(beverage2); beverage2.GetDescription(); Console.WriteLine("{0} costs {1}$", beverage2.GetDescription(), beverage2.cost()); Beverage beverage3 = new Decaf(); beverage3.setSize(Beverage.Size.Venti); beverage3 = new Soy(beverage3); beverage3 = new Whip(beverage3); Console.WriteLine("{0} costs {1}$", beverage3.GetDescription(), beverage3.cost()); Console.ReadLine(); }
static void Main(string[] args) { Beverage beverage = new Espresso(); Console.WriteLine(beverage.GetDescription() + " $ " + beverage.Cost()); beverage = new Mocha(beverage); Console.WriteLine(beverage.GetDescription() + " $ " + beverage.Cost()); beverage = new Mocha(beverage); Console.WriteLine(beverage.GetDescription() + " $ " + beverage.Cost()); beverage = new Whip(beverage); Console.WriteLine(beverage.GetDescription() + " $ " + beverage.Cost()); beverage.size = Size.Larger; Console.WriteLine(beverage.GetDescription() + " $ " + beverage.Cost()); beverage.size = Size.Middle; Console.WriteLine(beverage.GetDescription() + " $ " + beverage.Cost()); beverage.size = Size.Small; Console.WriteLine(beverage.GetDescription() + " $ " + beverage.Cost()); }
static void Main(string[] args) { Beverage beverage = new Espresso(); Console.WriteLine(beverage.GetDescription() + "$" + beverage.cost().ToString()); Beverage beverage2 = new DarkRoast(); beverage2 = new Mocha(beverage2); beverage2 = new Mocha(beverage2);//用两个Mocha修饰 beverage2 = new Whip(beverage2);//在用Whip修饰。 Console.WriteLine(beverage2.GetDescription() + "$" + beverage2.cost().ToString()); Console.ReadKey(); }