static void Main(string[] args)
        {
            /*
             * More info: https://en.wikipedia.org/wiki/Decorator_pattern
             *   What problems can the Decorator design pattern solve?
             *      Responsibilities should be added to (and removed from) an object dynamically at run-time.
             *      A flexible alternative to subclassing for extending functionality should be provided.
             *
             *  When using subclassing, different subclasses extend a class in different ways.
             *  But an extension is bound to the class at compile-time and can't be changed at run-time.
             */

            void PrintInfo(ICoffee coffee) => Console.WriteLine("Cost: " + coffee.GetCost() + "; Ingredients: " + coffee.GetIngredients());

            ICoffee c = new SimpleCoffee();

            PrintInfo(c);

            c = new CoffeeWithMilk(c);
            PrintInfo(c);

            c = new CoffeeWithSprinkles(c);
            PrintInfo(c);



            var coffeeWithMilk = new CoffeeWithMilk(new SimpleCoffee());

            PrintInfo(coffeeWithMilk);
        }
Exemple #2
0
        static void Main(string[] args)
        {
            ICoffee simpleCoffee = new SimpleCoffee();

            Console.WriteLine("Simple coffee:");
            Console.WriteLine("cost: " + simpleCoffee.getCost());
            Console.WriteLine("ingredients: " + simpleCoffee.getIngredients());

            ICoffee coffeeWithSugar = new CoffeeSugarDecorator(simpleCoffee);

            Console.WriteLine("Coffee with sugar:");
            Console.WriteLine("cost: " + coffeeWithSugar.getCost());
            Console.WriteLine("ingredients: " + coffeeWithSugar.getIngredients());

            ICoffee coffeeWithSugarAndMilk = new CoffeeMilkDecorator(coffeeWithSugar);

            Console.WriteLine("Coffee with sugar and milk:");
            Console.WriteLine("cost: " + coffeeWithSugarAndMilk.getCost());
            Console.WriteLine("ingredients: " + coffeeWithSugarAndMilk.getIngredients());
        }
Exemple #3
0
        static void Main(string[] args)
        {
            var myCoffee = new SimpleCoffee();

            Console.WriteLine($"{myCoffee.GetCost():c}"); // $ 5.00
            Console.WriteLine(myCoffee.GetDescription()); // Simple Coffee

            var milkCoffee = new MilkCoffee(myCoffee);

            Console.WriteLine($"{milkCoffee.GetCost():c}"); // $ 6.00
            Console.WriteLine(milkCoffee.GetDescription()); // Simple Coffee, milk

            var whipCoffee = new WhipCoffee(milkCoffee);

            Console.WriteLine($"{whipCoffee.GetCost():c}"); // $ 7.00
            Console.WriteLine(whipCoffee.GetDescription()); // Simple Coffee, milk, whip

            var vanillaCoffee = new VanillaCoffee(whipCoffee);

            Console.WriteLine($"{vanillaCoffee.GetCost():c}"); // $ 8.00
            Console.WriteLine(vanillaCoffee.GetDescription()); // Simple Coffee, milk, whip, vanilla
            Console.ReadKey();
        }