static void Main(string[] args) { #region Using ObserverPattern WeatherData weatherData = new WeatherData(); CurrentConditionsDisplay display = new CurrentConditionsDisplay(weatherData); weatherData.setMeasurements(80, 65, 30.4f); weatherData.setMeasurements(82, 70, 29.2f); weatherData.setMeasurements(78, 90, 29.2f); #endregion #region Using Strategy pattern Duck duck = new Duck(); duck.setFlyBehavior(new FlyWithWings()); duck.setQuackBehavior(new Quack()); duck.performFly(); duck.performQuack(); Duck duck2 = new Duck(); duck2.setFlyBehavior(new FlyWithWings()); duck2.setQuackBehavior(new Squeakcs()); duck2.performQuack(); duck2.performFly(); #endregion #region Using Decorator pattern 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()); #endregion // Don't close console on program end Console.ReadKey(); }
/// <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()); }