コード例 #1
0
        static void Main(string[] args)
        {
            /*
             * Design principle: Classes should be open for extension, but closed for modification.
             *
             * The Decorator Pattern: attaches additional responsabilities to an object dynamically.
             * Decorators provide a flexible alternative to subclassing for extending functionality.
             */

            Beverage beverage = new Expresso();

            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()}");

            Console.ReadKey();
        }
コード例 #2
0
        static void Main(string[] args)
        {
            Coffe coffe = new Expresso();   // Recursion is the fact behind. which keeps track of the encapsulated objects.

            coffe = new Mocha(coffe);       // In this line coffe = Mocha at runtime.
            coffe = new Milk(coffe);        // In this line coffe = Milk  at runtime.
            coffe = new Mocha(coffe);       // In this line coffe = Mocha at runtime.

            Console.WriteLine("Ingredients: " + coffe.GetDescription());
            Console.WriteLine("Total Cost: " + coffe.Cost());
        }
コード例 #3
0
        static void Main(string[] args)
        {
            //Order a Latte with Cream
            Coffee latteWithCream = new Latte();
            latteWithCream = new WhipCream(latteWithCream);
            Console.WriteLine(latteWithCream.GetDescription() + " at $" + latteWithCream.GetCost());

            //Order an Expresso with Caramel and Cream
            Coffee expressoWithCaramelAndCream = new Expresso();
            expressoWithCaramelAndCream = new WhipCream(expressoWithCaramelAndCream);
            expressoWithCaramelAndCream = new Caramel(expressoWithCaramelAndCream);
            Console.WriteLine(expressoWithCaramelAndCream.GetDescription() + " at $" + expressoWithCaramelAndCream.GetCost());
        }
コード例 #4
0
        static void Main(string[] args)
        {
            //Order a Latte with Cream
            Coffee latteWithCream = new Latte();

            latteWithCream = new WhipCream(latteWithCream);
            Console.WriteLine(latteWithCream.GetDescription() + " at $" + latteWithCream.GetCost());

            //Order an Expresso with Caramel and Cream
            Coffee expressoWithCaramelAndCream = new Expresso();

            expressoWithCaramelAndCream = new WhipCream(expressoWithCaramelAndCream);
            expressoWithCaramelAndCream = new Caramel(expressoWithCaramelAndCream);
            Console.WriteLine(expressoWithCaramelAndCream.GetDescription() + " at $" + expressoWithCaramelAndCream.GetCost());
        }
コード例 #5
0
ファイル: Program.cs プロジェクト: ssrajan/Projects
        static void Main(string[] args)
        {
            Beverage beverage = new Expresso();

            Console.WriteLine(beverage.getDescription() + " £" + beverage.cost());

            Beverage beverage2 = new DarkRoast();

            beverage2 = new Whip(beverage2);
            beverage2 = new Mocha(beverage2);
            beverage2 = new Whip(beverage2);
            Console.WriteLine(beverage2.getDescription() + " £" + beverage2.cost());

            Beverage beverage3 = new Mocha(new Mocha(new DarkRoast()));

            Console.WriteLine(beverage3.getDescription() + " £" + beverage3.cost());


            Console.ReadKey();
        }
コード例 #6
0
        static void Main(string[] args)
        {
            var client = new Client();

            var expresso = new Expresso();

            //Only Expresso
            client.ClientCode(expresso);
            Console.WriteLine();
            //Expresso with caramel
            var test = new Caramel(expresso);

            //Expresso with caramel and chocolate
            var test2 = new Chocolate(new Caramel(expresso));

            client.ClientCode(test);
            Console.WriteLine();

            client.ClientCode(test2);
            Console.WriteLine();

            Console.ReadLine();
        }