Пример #1
0
        /// <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()}");
        }
Пример #2
0
        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());
        }
Пример #3
0
        public static void main()
        {
            bevarages bev = new Espresso();

            Console.WriteLine(bev.description() + bev.Cost().ToString());
            bev = new Mocha(bev);
            Console.WriteLine(bev.description() + bev.Cost().ToString());
            bev = new Mocha(bev);
            Console.WriteLine(bev.description() + bev.Cost().ToString());
            bev = new soya(bev);
            Console.WriteLine(bev.description() + bev.Cost().ToString());
            Console.ReadKey();
        }
Пример #4
0
        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();
        }
Пример #5
0
        /// <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());
        }
Пример #6
0
        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());
        }
Пример #7
0
        private static void Decorator()
        {
            Beverage oBeverage2 = default(Beverage);

            oBeverage2 = new DarkRoast();

            //Agrega condimentos (tecnica Composition)
            oBeverage2 = new Mocha(oBeverage2);
            oBeverage2 = new Mocha(oBeverage2);
            oBeverage2 = new Whip(oBeverage2);

            Console.WriteLine(oBeverage2.getFullDescription() + " $" + oBeverage2.getTotalCost());

            Beverage beverage3 = new HouseBlend();

            beverage3 = new Soy(beverage3);
            beverage3 = new Mocha(beverage3);
            beverage3 = new Whip(beverage3);
            beverage3 = new Whip(beverage3);

            //Console.WriteLine(beverage3.getFullDescription() + " $" + beverage3.getTotalCost());
            Console.Read();
        }