コード例 #1
0
        /// <summary>
        /// Definition:
        /// The decorator pattern, ataches additional responsibility to an object dynamically. Decorators provides a flexible
        /// alternative to subclassing for extending functionality.
        /// </summary>
        static void Main(string[] args)
        {
            Beverage espressoWithCaramelAndSoy = new Caramel(new Soy(new Espresso()));
            Beverage espressoWithCaramel       = new Caramel(new Espresso());
            Beverage decafWithSoyAndCaramel    = new Soy(new Caramel(new Decaf()));

            Console.WriteLine($"Espresso with caramel and soy {espressoWithCaramelAndSoy.Cost()}$");
            Console.WriteLine($"Espresso with caramel {espressoWithCaramel.Cost()}$");
            Console.WriteLine($"Decaf with soy and caramel {decafWithSoyAndCaramel.Cost()}$");

            Console.ReadKey();
        }
コード例 #2
0
        public static void Main(string[] args)
        {
            var coffee = new Cream(new Caramel(new Cappichino()));

            System.Console.WriteLine(coffee.GetDescription());
            System.Console.WriteLine(coffee.Cost());

            var coffee1 = new Cream(new Cappichino());

            System.Console.WriteLine(coffee1.GetDescription());
            System.Console.WriteLine(coffee1.Cost());

            var coffee2 = new Cappichino();

            System.Console.WriteLine(coffee2.GetDescription());
            System.Console.WriteLine(coffee2.Cost());

            var coffee3 = new Caramel(new Cappichino());

            System.Console.WriteLine(coffee3.GetDescription());
            System.Console.WriteLine(coffee3.Cost());
        }