static void Main(string[] args) { IBeverage beverage = new DarkRoast(); System.Console.WriteLine($"{beverage.GetDescription()}: ${beverage.GetCost()}\n"); beverage = new Soy(beverage); System.Console.WriteLine($"{beverage.GetDescription()}: ${beverage.GetCost()}\n"); beverage = new Whip(beverage); System.Console.WriteLine($"{beverage.GetDescription()}: ${beverage.GetCost()}\n"); beverage = new Mocha(beverage); System.Console.WriteLine($"{beverage.GetDescription()}: ${beverage.GetCost()}\n"); beverage = new Mocha(beverage); System.Console.WriteLine($"{beverage.GetDescription()}: ${beverage.GetCost()}\n"); beverage = new Whip(beverage); System.Console.WriteLine($"{beverage.GetDescription()}: ${beverage.GetCost()}\n"); beverage = new Whip(beverage); System.Console.WriteLine($"{beverage.GetDescription()}: ${beverage.GetCost()}\n"); beverage = new Whip(beverage); System.Console.WriteLine($"{beverage.GetDescription()}: ${beverage.GetCost()}\n"); beverage = new Whip(beverage); System.Console.WriteLine($"{beverage.GetDescription()}: ${beverage.GetCost()}\n"); beverage = new Whip(beverage); System.Console.WriteLine($"{beverage.GetDescription()}: ${beverage.GetCost()}\n"); beverage = new Whip(beverage); System.Console.WriteLine($"{beverage.GetDescription()}: ${beverage.GetCost()}\n"); Console.ReadLine(); }
/// <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()}"); }
public void CostShouldReturnCorrectValue() { var expectedValue = 0.99M; var expresso = new DarkRoast(); expresso.Cost().Should().Be(expectedValue); }
public void add_milk_should_add_milk_to_coffee() { var darkRoast = new DarkRoast(); var coffee = new AddMilkCommand().Execute(darkRoast); coffee.ShouldBeType<MilkDecorator>(); }
public void add_sugar_should_add_sugar_to_coffee() { var darkRoast = new DarkRoast(); var coffee = new AddSugarCommand().Execute(darkRoast); coffee.ShouldBeType<SugarDecorator>(); }
public void CostIsComputedWithMocha() { DarkRoast darkRoast = new DarkRoast(); CondimentDecorator darkRoastWithMocha = new Mocha(darkRoast); Assert.That(darkRoastWithMocha.Cost(), Is.EqualTo(2.5)); }
static void Main(string[] args) { Beverage beverage = new Expresso(); Console.WriteLine("Beverage #1: " + beverage.getDescription() + " $" + beverage.cost()); Beverage beverage2 = new DarkRoast(); beverage2 = new Mocha(beverage2); beverage2 = new Mocha(beverage2); beverage2 = new Whip(beverage2); Console.WriteLine("\nBeverage #2: " + beverage2.getDescription() + " $" + beverage2.cost()); Beverage beverage3 = new HouseBlend(); beverage3 = new Soy(beverage3); beverage3 = new Mocha(beverage3); beverage3 = new Whip(beverage3); Console.WriteLine("\nBeverage #3: " + beverage3.getDescription() + " $" + beverage3.cost()); //keep window open Console.ReadLine(); }
static void Main(string[] args) { Beverage beverage1 = new Espresso(); Console.WriteLine(beverage1); Beverage beverage1Small = new Espresso(); beverage1Small.SetSize(CupSize.Small); Console.WriteLine(beverage1Small); Beverage beverage2 = new DarkRoast(); beverage2 = new Mocha(beverage2); beverage2 = new Mocha(beverage2); beverage2 = new Whip(beverage2); Console.WriteLine(beverage2); Beverage beverage3 = new HouseBlend(); beverage3 = new Soy(beverage3); beverage3 = new Mocha(beverage3); beverage3 = new Whip(beverage3); Console.WriteLine(beverage3); Beverage beverage3Big = new HouseBlend(CupSize.Big); beverage3Big = new Soy(beverage3Big); beverage3Big = new Mocha(beverage3Big); beverage3Big = new Whip(beverage3Big); Console.WriteLine(beverage3Big); }
static void Main(string[] args) { Beverage beverage = new Expresso(); Console.WriteLine(beverage.GetDescription() + " R$" + beverage.Cost()); Console.WriteLine(); Beverage beverage2 = new DarkRoast(); beverage2 = new Mocha(beverage2); beverage2 = new Mocha(beverage2); beverage2 = new Whip(beverage2); Console.WriteLine(beverage2.GetDescription() + " R$" + beverage2.Cost()); Console.WriteLine(); Beverage beverage3 = new HouseBlend(); beverage3 = new Soy(beverage3); beverage3 = new Mocha(beverage3); beverage3 = new Whip(beverage3); Console.WriteLine(beverage3.GetDescription() + " R$" + beverage3.Cost()); Console.WriteLine(); Console.ReadKey(); }
public void DecoratorTest() { DIServiceLocator.Current.RegisterInstance(typeof(ITextProvider)); Support.Configure(); Beverage beverage = new Espresso(); beverage = new Mocha(beverage); Assert.AreEqual(beverage.Description, "Espresso, Mocha"); Assert.AreEqual(beverage.Cost(), 2.19); Beverage beverage2 = new DarkRoast(); beverage2 = beverage2.AddComponent <Mocha>(); beverage2 = beverage2.AddComponent <Mocha>(); beverage2 = beverage2.AddComponent <Whip>(); Assert.AreEqual(beverage2.Description, "Dark Roast, Mocha, Mocha, Whip"); Assert.AreEqual(beverage2.Cost(), 1.49); Beverage beverage3 = new HouseBlend(); SupportExtensions.AddComponent <Soy>(ref beverage3); SupportExtensions.AddComponent <Mocha>(ref beverage3); SupportExtensions.AddComponent <Whip>(ref beverage3); Assert.AreEqual(beverage3.Description, "House Blend Coffee, Soy, Mocha, Whip"); Assert.AreEqual(beverage3.Cost(), 1.34); }
public void DescriptionWithMochaAndEspresso() { DarkRoast darkRoast = new DarkRoast(); CondimentDecorator darkRoastWithMocha = new Mocha(darkRoast); CondimentDecorator darkRoastWithMochaAndMilk = new Milk(darkRoastWithMocha); Assert.That(darkRoastWithMochaAndMilk.Description, Is.EqualTo("The Best Dark Roast, mocha, milk")); }
public void CostIsComputedWithMochaAndEspresso() { DarkRoast darkRoast = new DarkRoast(); CondimentDecorator darkRoastWithMocha = new Mocha(darkRoast); CondimentDecorator darkRoastWithMochaAndMilk = new Milk(darkRoastWithMocha); Assert.That(darkRoastWithMochaAndMilk.Cost(), Is.EqualTo(3.25)); }
public void DarkRoastWithMochaWithWhipWithMilk() { DarkRoast beverage = new DarkRoast(); beverage.HasMocha = true; beverage.HasWhip = true; beverage.HasMilk = true; Assert.AreEqual(1.39, beverage.Cost(), DELTA); }
public void should_save_dark_roast() { var darkRoast = new DarkRoast(); darkRoast.Save(); Repository<DarkRoast>.LastItemSaved.ShouldBe(darkRoast); }
public void GetDescriptionShouldReturnDescriptionWithTextSettedInConstructor() { var expectedValue = "Dark Roast Coffee"; var expresso = new DarkRoast(); expresso.GetDescription().Should() .Be(expectedValue); }
public void DarkRoastWithMochaWithWhipWithMilk() { Beverage beverage = new DarkRoast(); beverage = new Mocha(beverage); beverage = new Whip(beverage); beverage = new Milk(beverage); Assert.AreEqual(1.39, beverage.Cost(),DELTA); }
public void Cost_WithoutCondiments_ReturnNinetyNineCents() { var darkRoastCoffee = new DarkRoast(); var cost = darkRoastCoffee.Cost(); cost.Should().Be(0.99m); }
public void price_should_be_7() { var darkRoast = new DarkRoast(); var price = darkRoast.GetPrice(); Assert.AreEqual(7m, price); }
public void description_should_be_dark_roast() { var darkRoast = new DarkRoast(); var desc = darkRoast.GetDescription(); Assert.AreEqual("Dark Roast", desc); }
public void TestDarkRoast() { Beverage beverage = new DarkRoast(); beverage = new Mocha(beverage); beverage = new Soy(beverage); Assert.AreEqual("Dark Roast Coffee, Mocha, Soy $1.34", beverage.GetDescription() + " $" + beverage.Cost()); }
static void Main(string[] args) { Beverage beverage1 = new DarkRoast(); beverage1 = new Milk(beverage1); beverage1 = new Whip(beverage1); Console.WriteLine("Total Price: {0}, Description: {1}", beverage1.Cost(), beverage1.GetDescription); }
public void should_be_able_to_make_dark_roast_and_add_milk_with_delegate_command() { ICoffee coffee = new DarkRoast(); coffee = new DelegateCommand(c => c.AddSugar()).Execute(coffee); coffee = new DelegateCommand(c => c.AddMilk()).Execute(coffee); coffee.ShouldBeType<MilkDecorator>(); }
public void DarkRoastWithMochaWithWhipWithMilk() { Beverage beverage = new DarkRoast(); beverage = new Mocha(beverage); beverage = new Whip(beverage); beverage = new Milk(beverage); Assert.AreEqual(1.39, beverage.Cost(), DELTA); }
public void DarkRoastMochaWithWhip() { IBeverage roast = new DarkRoast(BeverageSize.Large); roast = new Mocha(roast); roast = new Whip(roast); Debug.WriteLine(roast.Description); Debug.WriteLine("Cost: " + roast.Cost); }
public void Cost_WithMocha_ReturnOneDollarAndNineteenCents() { Beverage darkRoastCoffee = new DarkRoast(); darkRoastCoffee = new Mocha(darkRoastCoffee); var cost = darkRoastCoffee.Cost(); cost.Should().Be(1.19m); }
public void Main() { Beverage beverage = new Espresso(); Console.WriteLine(beverage.GetDescription() + " $" + beverage.Cost()); Beverage beverage1 = new DarkRoast(); beverage1 = new Mocha(beverage1); }
public static void Run() { Beverage beverage = new Espresso(); Console.WriteLine(beverage.description + " $" + beverage.cost()); Console.WriteLine(); Beverage beverage2 = new DarkRoast(); beverage2 = new Mocha(beverage2); beverage2 = new Mocha(beverage2); beverage2 = new Whip(beverage2); Console.WriteLine(beverage2.description + " $" + beverage2.cost()); Console.WriteLine(); Beverage beverage3 = new HouseBlend(); beverage3 = new Soy(beverage3); beverage3 = new Mocha(beverage3); beverage3 = new Whip(beverage3); Console.WriteLine(beverage3.description + " $" + beverage3.cost()); Console.WriteLine(); Beverage beverage4 = new HouseBlend(); beverage4.size = Beverage.Size.GRANDE; Console.WriteLine("Setting size to: {0}", beverage4.size); beverage4 = new Soy(beverage4); beverage4 = new Mocha(beverage4); beverage4 = new Whip(beverage4); Console.WriteLine(beverage4.description + " $" + beverage4.cost()); Console.WriteLine(); Beverage beverage5 = new HouseBlend(); beverage5.size = Beverage.Size.TALL; Console.WriteLine("Setting size to: {0}", beverage5.size); beverage5 = new Soy(beverage5); beverage5 = new Mocha(beverage5); beverage5 = new Whip(beverage5); Console.WriteLine(beverage5.description + " $" + beverage5.cost()); Console.WriteLine(); Beverage beverage6 = new HouseBlend(); beverage6.size = Beverage.Size.VENTI; Console.WriteLine("Setting size to: {0}", beverage6.size); beverage6 = new Soy(beverage6); beverage6 = new Mocha(beverage6); beverage6 = new Whip(beverage6); Console.WriteLine(beverage6.description + " $" + beverage6.cost()); Console.WriteLine(); Console.ReadKey(); }
public void Test() { DarkRoast darkRoast = new DarkRoast(); Milk milk = new Milk(darkRoast); Mocha mocha = new Mocha(milk); Soy soy = new Soy(mocha); Whip whip = new Whip(soy); Assert.Equal("Dark Roast, Milk, Mocha, Soy, Whip", whip.GetDescription()); }
public void DarkRoastPriceTest() { //Arrange Beverage bev = new DarkRoast(); decimal price; //Act price = bev.GetPrice(); //Assert Assert.AreEqual(price, 0.99m); }
static void Main(string[] args) { var houseBlend = new HouseBlend(); houseBlend.Prepare(); Console.ReadLine(); var darkRoast = new DarkRoast(); darkRoast.Prepare(); Console.ReadLine(); }
public void DecoratorTest() { { var m = new ConcreteComponent(); var decoratorA = new DecoratorA(m); // decoratorA.Work(); var decoratorB = new DecoratorB(decoratorA); decoratorB.Work(); } { // 我买了个苹果手机 Phone phone = new ApplePhone(); // 现在想贴膜了 var applePhoneWithSticker = new Sticker(phone); // 扩展贴膜行为 applePhoneWithSticker.Print(); // 现在我想有挂件了 var applePhoneWithAccessories = new Accessories(phone); // 扩展手机挂件行为 applePhoneWithAccessories.Print(); Console.WriteLine("----------------------"); // 现在我同时有贴膜和手机挂件了 var sticker = new Sticker(phone); var applePhoneWithAccessoriesAndSticker = new Accessories(sticker); applePhoneWithAccessoriesAndSticker.Print(); } // { // AbstractMessage message = new SqlMessage(); // var de = new CheckUserWrapper(message); // Console.WriteLine(de.Get()[0].Message); // // var d2 = new CheckInputWrapper(message); // Console.WriteLine(d2.Get()[0].Message); // // var m3 = new CheckUserWrapper(message); // var m4 = new CheckInputWrapper(m3); // Console.WriteLine(m4.Get()[0].Message); // } { var dark = new DarkRoast("黑咖啡"); var sugar = new Sugar("加糖", dark); Console.WriteLine($"{sugar.GetDescription()}{sugar.Cost()}元");; var milk = new Milk("加奶", sugar); Console.WriteLine($"{milk.GetDescription()}{milk.Cost()}元");; } }
public void DifferentSizesOfSoy_ShouldHaveDifferentCosts() { IBeverage tallBeverage = new DarkRoast(); tallBeverage = new Soy(tallBeverage); Assert.AreEqual(1.09, tallBeverage.Cost()); //.99 + .10 IBeverage grandeBeverage = new DarkRoast(); grandeBeverage.Size = BeverageSize.Grande; grandeBeverage = new Soy(tallBeverage); Assert.AreEqual(1.14, tallBeverage.Cost()); //.99 + .15 }
public void DarkRoastWithSoyPriceTest() { //Arrange Beverage bev = new DarkRoast(); bev = new Soy(bev); decimal price; //Act price = bev.GetPrice(); //Assert Assert.AreEqual(price, 1.14m); }
public void DarkRoastWithMochaPriceTest() { //Arrange Beverage bev = new DarkRoast(); bev = new SteamedMilk(bev); decimal price; //Act price = bev.GetPrice(); //Assert Assert.AreEqual(price, 1.09m); }
static void DecoratorInvoke() { Beverage beverage = new DarkRoast(); // 焦烤咖啡 Console.WriteLine(beverage.GetDescription() + " $" + beverage.Cost()); beverage = new Mocha(beverage); //添加摩卡 Console.WriteLine(beverage.GetDescription() + " $" + beverage.Cost()); beverage = new Milk(beverage); //添加牛奶 Console.WriteLine(beverage.GetDescription() + " $" + beverage.Cost()); Beverage beverage2 = new Milk(new HouseBlend()); // 家庭混合咖啡加摩卡加牛奶 Console.WriteLine(beverage2.GetDescription() + " $" + beverage2.Cost()); }
public void DarkRoast_ShouldBeExpressoAndCostNinetyNineCents_WhenIsInstantiate() { //Arrange Beverage beverage = new DarkRoast(); //Act var description = beverage.GetDescription(); var cost = beverage.Cost(); //Assert description.Should().Be("Dark Roast Coffee"); cost.Should().Be(.99); }
public void BeverageObjectDescription_ReturnsCorrectName() { IBeverage roast = new DarkRoast(); Assert.AreEqual("Dark Roast Coffee", roast.Description); IBeverage coffee = new SimpleCoffee(); Assert.AreEqual("Simple Black Coffee", coffee.Description); IBeverage espresso = new Espresso(); Assert.AreEqual("Espresso", espresso.Description); }
static void Main(string[] args) { Beverage beverage = new Espresso(); System.Console.WriteLine(beverage.getDescription() + " $" + beverage.cost()); Beverage beverage2 = new DarkRoast(); beverage2 = new Mocha(beverage2); beverage2 = new Mocha(beverage2); beverage2 = new Whip(beverage2); System.Console.WriteLine(beverage2.getDescription() + " $" + beverage2.cost()); }
public IBeverage GetCoffee(StoreType storeType, BeverageType beverageType) { IBeverage coffee = null; switch(storeType) { case StoreType.RegularStore : switch (beverageType) { case BeverageType.HouseBlend: coffee = new HouseBlend(); break; case BeverageType.Roast: coffee = new DarkRoast(); break; } break; case StoreType.ExoticStore: switch (beverageType) { case BeverageType.HouseBlend: coffee = new SpecialHouseBlend(); break; case BeverageType.Roast: coffee = new AmazonRoast(); break; } break; } return coffee; }
private static void WithDecorator() { IBeverage roast = new DarkRoast(); Console.WriteLine("{0} cost {1}", roast.GetDescription(), roast.GetPrice()); Console.ReadLine(); roast = new MilkDecorator(roast); Console.WriteLine("{0} cost {1}", roast.GetDescription(), roast.GetPrice()); Console.ReadLine(); roast = new SugarDecorator(roast); Console.WriteLine("{0} cost {1}", roast.GetDescription(), roast.GetPrice()); Console.ReadLine(); IBeverage house = new HouseBlend(); house = new MilkDecorator(house); Console.WriteLine("{0} cost {1}", house.GetDescription(), house.GetPrice()); Console.ReadLine(); }
public void adding_milk_using_extension_should_update_dark_roast_price() { var darkRoast = new DarkRoast().AddMilk(); darkRoast.GetPrice().ShouldBe(7.5m); }