public void Expresso_WhenInstantiated_ReturnsCorrectValues() { Beverage beverage = new Espresso(); Assert.Equal(nameof(Espresso), beverage.GetDescription()); Assert.Equal(1.99m, beverage.Value()); }
/// <summary> /// # Motivation /// I can now attach responsibilities to an object dynamically to add behaviour at runtime. /// /// # Structure /// Beverage /// --> base class so all objects are of same type /// CondimentDecorator : Bevarage /// --> interchangeable with Beverage /// /// # Sample Problem /// A coffee shop wants to make an ordering system for all their drinks. Ofcourse there are a lot of combination with condiments/roasts. /// We could create a base class and let all possible combinations inherit from the superclass and override their behaviour, but that would quickly become a mess. /// /// # Solution /// By creating a Decorator class which inherits from the baseclass (to effectively swap between types), I can now dynamically create composite objects at runtime. /// To add a drink to the system: /// 1. inherit from the base class, set instance fields in constructor /// 2. override the 'Cost()' method to return desired cost /// To add a condiment to the system: /// 1. inherit from 'CondimentDecorator' (indirectly from base class as well /// 2. add instance field from type Beverage /// 3. initialize in constructor /// 4. override GetDescription() and Cost() accordingly /// To create a composite object at runtime: /// 1. Create a new instance of a drink (Beverage) /// 2. To add a condiment, assign the instance to a new instance of the condiment with itself as param /// A. Beverage darkRoast = new DarkRoast(); /// B. darkRoast = new Mocha(darkRoast); /// </summary> public void Test() { Beverage beverage = new Espresso(); Console.WriteLine($"{beverage.GetDescription()} ${beverage.Cost()}"); Beverage beverage1 = new DarkRoast(); beverage1 = new Mocha(beverage1); Console.WriteLine($"{beverage1.GetDescription()} ${beverage1.Cost()}"); Beverage beverage2 = new DarkRoast(); beverage2 = new Mocha(beverage2); Console.WriteLine($"{beverage2.GetDescription()} ${beverage2.Cost()}"); beverage2 = new Mocha(beverage2); Console.WriteLine($"{beverage2.GetDescription()} ${beverage2.Cost()}"); beverage2 = new Whip(beverage2); Console.WriteLine($"{beverage2.GetDescription()} ${beverage2.Cost()}"); Beverage beverage3 = new HouseBlend(); beverage3 = new Soy(beverage3); beverage3 = new Mocha(beverage3); beverage3 = new Whip(beverage3); Console.WriteLine($"{beverage3.GetDescription()} ${beverage3.Cost()}"); }
static void Main(string[] args) { Beverage beverage = new Espresso(); Console.WriteLine("{0} ${1}", beverage.GetDescription(), beverage.Cost()); beverage.SetSize(3); Console.WriteLine("{0} ${1}", beverage.GetDescription(), beverage.Cost()); Beverage beverage2 = new Mocha(beverage); Console.WriteLine("{0} ${1}", beverage2.GetDescription(), beverage2.Cost()); beverage2 = new Mocha(beverage2); Console.WriteLine("{0} ${1}", beverage2.GetDescription(), beverage2.Cost()); beverage2.SetSize(2); Console.WriteLine("{0} ${1}", beverage2.GetDescription(), beverage2.Cost()); Console.ReadKey(); }
public void Main() { Beverage beverage = new Espresso(); Console.WriteLine(beverage.GetDescription() + " $" + beverage.Cost()); Beverage beverage1 = new DarkRoast(); beverage1 = new Mocha(beverage1); }
private static void RunDecoratorPattern() { Beverage beverage = new Espresso(); Console.WriteLine($"{beverage.GetDescription()} ${beverage.Cost()}"); Beverage beverage1 = new HouseBlend(); beverage1 = new Mocha(beverage1); beverage1 = new Mocha(beverage1); beverage1 = new Whip(beverage1); Console.WriteLine($"{beverage1.GetDescription()} ${beverage1.Cost()}"); }
public void StarBuzzCoffee() { Beverage espresso = new Espresso(); Console.WriteLine(espresso.GetDescription() + " $" + espresso.Cost()); Beverage houseBlend = new HouseBlend(); houseBlend = new Soy(houseBlend); houseBlend = new Mocha(houseBlend); houseBlend = new Whip(houseBlend); Console.WriteLine(houseBlend.GetDescription() + " $" + houseBlend.Cost()); }
// Update is called once per frame void Update() { if (Input.GetKeyDown(KeyCode.A)) { ICoffee coffee = new Espresso(); Debug.Log("Coffee discription and cost: " + coffee.GetDescription() + " $" + coffee.GetCost()); } if (Input.GetKeyDown(KeyCode.S)) { ICoffee coffee = new Filtered(); Debug.Log("Coffee discription and cost: " + coffee.GetDescription() + " $" + coffee.GetCost()); } if (Input.GetKeyDown(KeyCode.D)) { ICoffee coffee = new ChocolateDecorator(new Espresso()); Debug.Log("Coffee discription and cost: " + coffee.GetDescription() + " $" + coffee.GetCost()); } if (Input.GetKeyDown(KeyCode.F)) { ICoffee coffee = new ChocolateDecorator(new Filtered()); Debug.Log("Coffee discription and cost: " + coffee.GetDescription() + " $" + coffee.GetCost()); } if (Input.GetKeyDown(KeyCode.Z)) { ICoffee coffee = new MilkDecorator(new Espresso()); Debug.Log("Coffee discription and cost: " + coffee.GetDescription() + " $" + coffee.GetCost()); } if (Input.GetKeyDown(KeyCode.X)) { ICoffee coffee = new MilkDecorator(new Filtered()); Debug.Log("Coffee discription and cost: " + coffee.GetDescription() + " $" + coffee.GetCost()); } if (Input.GetKeyDown(KeyCode.C)) { ICoffee coffee = new ChocolateDecorator(new MilkDecorator(new Filtered())); Debug.Log("Coffee discription and cost: " + coffee.GetDescription() + " $" + coffee.GetCost()); } if (Input.GetKeyDown(KeyCode.V)) { ICoffee coffee = new ChocolateDecorator(new MilkDecorator(new Espresso())); Debug.Log("Coffee discription and cost: " + coffee.GetDescription() + " $" + coffee.GetCost()); } }
static void Main(string[] args) { Beverage beverage = new Espresso(); Console.WriteLine($"{beverage.GetDescription()} : {beverage.Cost()}"); //New beverage //Wrap it with a mocha, second mocha and then whip Beverage beverage2 = new Espresso(); beverage2 = new Mocha(beverage2); beverage2 = new Mocha(beverage2); beverage2 = new Whip(beverage2); Console.WriteLine($"{beverage2.GetDescription()} : {beverage2.Cost()}"); }
static void Main(string[] args) { Beverage beverage = new Espresso(); Console.WriteLine($"{beverage.GetDescription()} ${beverage.Cost()}"); Beverage beverage1 = new HouseBlend(); beverage1 = new Soy(beverage1); beverage1 = new Mocha(beverage1); beverage1 = new Mocha(beverage1); Console.WriteLine($"{beverage1.GetDescription()} ${beverage1.Cost()}"); Console.ReadLine(); }
public static void RunStarBuck() { Console.WriteLine("StarBuck Coffe Order: "); Beverage beverage = new Espresso(); Console.WriteLine(string.Format("{0} : ${1}", beverage.GetDescription(), beverage.GetCost())); Beverage beverage2 = new DarkRoast(); beverage2 = new Mocha(beverage2); beverage2 = new SteamMilk(beverage2); beverage2 = new Whip(beverage2); Console.WriteLine(string.Format("{0} : ${1}", beverage2.GetDescription(), beverage2.GetCost())); }
static void Main(string[] args) { Beverage beverage = new Espresso(); Console.WriteLine(beverage.GetDescription() + " $" + beverage.Cost()); Beverage beverage2 = new HouseBlend(); beverage2 = new Mocha(beverage2); beverage2 = new Soy(beverage2); beverage2 = new Whip(beverage2); Console.WriteLine(beverage2.GetDescription() + " $" + beverage2.Cost()); Beverage beverage3 = new DarkRoase(); beverage3 = new Mocha(beverage3); beverage3 = new Mocha(beverage3); beverage3 = new Whip(beverage3); Console.WriteLine(beverage3.GetDescription() + " $" + beverage3.Cost()); //Console.ReadLine(); Console.WriteLine("---------------------\n"); // 我买了个苹果手机 Phone phone = new ApplePhone(); // 现在想贴膜了 Decorator applePhoneWithSticker = new Sticker(phone); // 扩展贴膜行为 applePhoneWithSticker.Print(); Console.WriteLine("----------------------\n"); // 现在我想有挂件了 Decorator applePhoneWithAccessories = new Accessories(phone); // 扩展手机挂件行为 applePhoneWithAccessories.Print(); Console.WriteLine("----------------------\n"); // 现在我同时有贴膜和手机挂件了 Sticker sticker = new Sticker(phone); Accessories applePhoneWithAccessoriesAndSticker = new Accessories(sticker); applePhoneWithAccessoriesAndSticker.Print(); Console.ReadLine(); }
static void Main(string[] args) { Beverage.Beverage espresso = new Espresso(); Console.WriteLine($"{espresso.GetDescription()}, ${espresso.Cost()}"); Beverage.Beverage soyHouseBlend = new HouseBlend(); soyHouseBlend = new Soy(soyHouseBlend); Console.WriteLine($"{soyHouseBlend.GetDescription()}, ${soyHouseBlend.Cost()}"); Beverage.Beverage doubleMochaWithWhipInDarkRoast = new DarkRoast(); doubleMochaWithWhipInDarkRoast = new Mocha(doubleMochaWithWhipInDarkRoast); doubleMochaWithWhipInDarkRoast = new Mocha(doubleMochaWithWhipInDarkRoast); doubleMochaWithWhipInDarkRoast = new Whip(doubleMochaWithWhipInDarkRoast); Console.WriteLine($"{doubleMochaWithWhipInDarkRoast.GetDescription()}, ${doubleMochaWithWhipInDarkRoast.Cost()}"); Console.ReadKey(); }
static void Main() { Beverage beverage0 = new Espresso(); System.Console.WriteLine($"{beverage0.GetDescription()} {beverage0.Cost()} $"); Beverage beverage1 = new DarkRoast(); beverage1 = new Mocha(beverage1); beverage1 = new Mocha(beverage1); beverage1 = new Whip(beverage1); System.Console.WriteLine($"{beverage1.GetDescription()} {beverage1.Cost()} $"); Beverage beverage2 = new HouseBlend(); beverage2 = new Soy(beverage2); beverage2 = new Mocha(beverage2); beverage2 = new Whip(beverage2); System.Console.WriteLine($"{beverage2.GetDescription()} {beverage2.Cost()} $"); }
static void Main(string[] args) { Beverage beverage = new Espresso(); Console.WriteLine($"{beverage.GetDescription()} ${beverage.Cost()}"); Beverage beverage2 = new DarkRoast(); beverage2 = new Mocha(beverage2); beverage2 = new Mocha(beverage2); beverage2 = new Whip(beverage2); Console.WriteLine($"{beverage2.GetDescription()} ${beverage2.Cost()}"); Beverage beverage3 = new HouseBlend(); beverage3 = new Soy(beverage3); beverage3 = new Mocha(beverage3); beverage3 = new Whip(beverage3); Console.WriteLine($"{beverage3.GetDescription()} ${beverage3.Cost()}"); }
static void Decorator() { Beverage beverage1 = new Espresso(); Console.WriteLine(beverage1.GetDescription()); Console.WriteLine(beverage1.GetCost()); Beverage beverage2 = new Espresso(); beverage2 = new Mocha(beverage2); Console.WriteLine(beverage2.GetDescription()); Console.WriteLine(beverage2.GetCost()); Beverage beverage3 = new Espresso(); beverage3 = new Mocha(beverage3); beverage3 = new Whip(beverage3); Console.WriteLine(beverage3.GetDescription()); Console.WriteLine(beverage3.GetCost()); }
static void Main(string[] args) { // Simulando o pedido de um Café Expresso sem condimentos Beverage b1 = new Espresso(); Console.WriteLine(b1.GetDescription() + " - R$ " + b1.Cost()); // Simulando o pedido de um Café DarkRoast com duplo Moca (chocolate) e creme Beverage b2 = new DarkRoast(); b2 = new Mocha(b2); //b2 = new Mocha(b2); b2 = new Whip(b2); Console.WriteLine(b2.GetDescription() + " - R$ " + b2.Cost()); // Simulando o pedido de um Café HouseBlend com Moca, soja e creme Beverage b3 = new HouseBlend(); b3 = new Soy(b3); b3 = new Mocha(b3); b3 = new Whip(b3); Console.WriteLine(b3.GetDescription() + " - R$ " + b3.Cost()); Console.ReadKey(); }
static void Main(string[] args) { Beverage beverage = new Espresso(); Console.WriteLine(beverage.GetDescription() + " $" + beverage.Cost()); Beverage beverageDark = new DarkRoast(); beverageDark = new Mocha(beverageDark); beverageDark = new Mocha(beverageDark); beverageDark = new Whip(beverageDark); Console.WriteLine(beverageDark.GetDescription() + " $" + beverageDark.Cost()); Beverage beverageHouse = new HouseBlend(); beverageHouse = new Mocha(beverageHouse); beverageHouse = new Soy(beverageHouse); beverageHouse = new Whip(beverageHouse); Console.WriteLine(beverageHouse.GetDescription() + " $" + beverageHouse.Cost()); Console.ReadLine(); }
static void Main(string[] args) { Beverage bev = new Espresso(); Console.WriteLine(bev.GetDescription() + "- $" + bev.cost()); Beverage bev2 = new DarkRoast(); bev2 = new Soy(bev2); bev2 = new Mocha(bev2); bev2 = new Soy(bev2); Console.WriteLine(bev2.GetDescription() + " - $" + bev2.cost()); Beverage bev3 = new HouseBlend(); bev3 = new Soy(bev3); bev3 = new Mocha(bev3); bev3 = new Whip(bev3); bev3 = new Soy(bev3); bev3 = new Whip(bev3); Console.WriteLine(bev3.GetDescription() + " - $" + bev3.cost()); 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); beverage2 = new Whip(beverage2); Console.WriteLine(beverage2.GetDescription() + ": " + beverage2.Cost().ToString()); Beverage beverage3 = new HouseBlend(); beverage3 = new Soy(beverage3); beverage3 = new Mocha(beverage3); beverage3 = new Whip(beverage3); Console.WriteLine(beverage3.GetDescription() + ": " + beverage3.Cost().ToString()); Console.ReadKey(); }