/// <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); 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 Main(string[] args) { Console.WriteLine("Strategy Pattern"); var modelDuck = new ModelDuck(); modelDuck.PerformFly(); modelDuck.PerformQuack(); modelDuck.SetFlyBehavior(new ModelFly()); modelDuck.PerformFly(); Console.WriteLine("Observer Patterns!"); var weatherData = new WeatherData(); var observerone = new OberverElement(weatherData); observerone.Subscript(); var observertwo = new OberverOtherElement(weatherData); observertwo.Subscript(); weatherData.SetMeasurementsChanged((float)35.50, (float)20.00, (float)264.00); observerone.UnSubscript(); weatherData.SetMeasurementsChanged((float)37.50, (float)30.00, (float)260.00); Console.WriteLine("Decorator Patterns"); var beverage = new Espresso(); Console.WriteLine(beverage.GetDescription()); BeverageBase beverage2 = new HouseBlend(); beverage2 = new Mocha(beverage2); beverage2 = new Whip(beverage2); Console.WriteLine(beverage2.GetDescription() + "$" + beverage2.Cost()); }
/// <summary> /// O padrão Decorator anexa responsabilidades adicionais a um objeto dinamicamente. Os decoradores fornecem uma alternativa mais flexível de /// subclasses para estender a funcionalidade. /// </summary> private static void TestDecorator() { Beverage beverage = new Espresso(); Console.WriteLine("{0}: {1}", beverage.Description, beverage.Cost()); Beverage beverage2 = new DarkRoast(); beverage2 = new Mocha(beverage2); beverage2 = new Mocha(beverage2); beverage2 = new Whip(beverage2); Console.WriteLine("{0}: {1}", beverage2.Description, beverage2.Cost()); Beverage beverage3 = new HouseBlend(); beverage3 = new Soy(beverage3); beverage3 = new Mocha(beverage3); beverage3 = new Whip(beverage3); Console.WriteLine("{0}: {1}", beverage3.Description, beverage3.Cost()); }
public static void UseDecoratorPattern(IView view) { Beverage espresso = new Espresso(); view.DisplayMessage(espresso.GetDescription() + " $" + espresso.Cost()); Beverage houseBlend = new HouseBlend(); //decorate the object houseBlend = new Mocha(houseBlend); houseBlend = new Whip(houseBlend); view.DisplayMessage(houseBlend.GetDescription() + " $" + houseBlend.Cost()); Beverage newEspresso = new Espresso(); newEspresso = new Mocha(newEspresso); newEspresso = new Soy(newEspresso); newEspresso = new Whip(newEspresso); view.DisplayMessage(newEspresso.GetDescription() + " $" + newEspresso.Cost()); }