Beispiel #1
0
        internal static void ExecuteDecorator()
        {
            //Intent
            //  • Attach additional responsibilities to an object dynamically.
            //      Decorators provide a flexible alternative to subclassing for extending
            //      functionality.
            //  • Client - specified embellishment of a core object by recursively
            //      wrapping it.
            //  • Wrapping a gift, putting it in a box, and wrapping the box.

            //Problem
            //  You want to add behavior or state to individual objects at run-time.
            //  Inheritance is not feasible because it is static and applies to an entire
            //  class.

            Beverage beverage  = new Cream(new Cream(new Espresso()));
            Beverage beverage2 = new Mocha(new Milk(new Americano()));


            Console.WriteLine(String.Format($"Beverage: {beverage.GetName()}"));
            Console.WriteLine(String.Format($"Cost: {beverage.GetCost()}"));

            Console.WriteLine(String.Format($"Beverage: {beverage2.GetName()}"));
            Console.WriteLine(String.Format($"Cost: {beverage2.GetCost()}"));
        }