Beispiel #1
0
            static void DecoratorCalling()
            {
                IPizza pizza           = new Pizza();
                IPizza cheeseDecorator = new CheeseDecorator(pizza);
                IPizza tomatoDecorator = new TomatoDecorator(cheeseDecorator);
                IPizza onionDecorator  = new OnionDecorator(tomatoDecorator);

                Console.WriteLine(tomatoDecorator.getPizzaType());
            }
Beispiel #2
0
        static void Main(string[] args)
        {
            /*
             * allow us to attach features in run time
             * we can decore an object to extend it usage
             *
             * 1 base interface
             * 2 concrete class
             * 3 base decortor class
             * 4 derived decorator class
             *
             */

            IPizza pizza     = new Pizza();
            IPizza chesseDec = new ChesseDecorator(pizza);
            IPizza tomatoDec = new TomatoDecorator(chesseDec);
            IPizza onionDec  = new OnionDecorator(tomatoDec);

            Console.WriteLine(onionDec.GetPizzaType());
            Console.WriteLine("Hello decorator!");
            Console.ReadLine();
        }
Beispiel #3
0
        public ActionResult Index(int?searchString)
        {
            var viewModel = new ViewModels.DepartmentViewModel();

            viewModel.Books = db.Books.Where(b => b.Year > searchString);

            ViewBag.Title = "Home Page";

            //Prototype
            PrototypeCalling();

            //Decorator
            DecoratorCalling();

            //Adapter
            AdapterCalling();

            //Composite
            CompositeCalling();

            //Proxy
            ProxyCalling();

            //Iterator
            IteratorCalling();

            //State
            StateCalling();

            //Command
            CommandCalling();

            //Mediator
            MediatorCalling();

            return(View(viewModel));

            void CommandCalling()
            {
                Console.WriteLine("Enter Commands (ON/OFF) : ");
                string   input      = "ON"; //Console.ReadLine();
                Light    light      = new Light();
                ICommand switchUp   = new FlipUpCommand(light);
                ICommand switchDown = new FlipDownCommand(light);

                CRUDWebApp.DesignPatterns.Switch s = new CRUDWebApp.DesignPatterns.Switch();
                if (input == "ON")
                {
                    s.StoreAndExecute(switchUp);
                }
                else if (input == "OFF")
                {
                    s.StoreAndExecute(switchDown);
                }
                else
                {
                    Debug.WriteLine("Command \"ON\" or \"OFF\" is required.");
                    //Console.log("Command \"ON\" or \"OFF\" is required.");
                }
            }

            void StateCalling()
            {
                Steak steak = new Steak("T-Bone");

                steak.AddTemp(120);
                steak.AddTemp(15);
                steak.AddTemp(15);
                steak.RemoveTemp(10);
                steak.RemoveTemp(15);
                steak.AddTemp(20);
                steak.AddTemp(20);
                steak.AddTemp(20);
            }

            void IteratorCalling()
            {
                // The client code may or may not know about the Concrete Iterator
                // or Collection classes, depending on the level of indirection you
                // want to keep in your program.
                var collection = new WordsCollection();

                collection.AddItem("First");
                collection.AddItem("Second");
                collection.AddItem("Third");
                Debug.WriteLine("Straight traversal:");
                foreach (var element in collection)
                {
                    Debug.WriteLine(element);
                }
                Debug.WriteLine("\nReverse traversal:");
                collection.ReverseDirection();
                foreach (var element in collection)
                {
                    Debug.WriteLine(element);
                }
            }

            void ProxyCalling()
            {
                var secureNestProxy = new SecureNestProxy(new RealNest());

                secureNestProxy.Access("Stegosaurus");
                secureNestProxy.Access("TRex");
            }

            void CompositeCalling()
            {
                var plants  = new List <IPlant>();
                var branchI = new Branch(new List <IPlant>()
                {
                    new Leaf(), new Leaf()
                });
                var branchII = new Branch(new List <IPlant>()
                {
                    new Leaf(), new Leaf(), new Leaf(), new Leaf()
                });

                plants.Add(new Branch(
                               new List <IPlant>()
                {
                    branchI, branchII
                }
                               ));      //branch with two branches
                plants.Add(new Leaf()); //one leaf
                plants.Add(new Branch(new List <IPlant>()
                {
                    new Leaf(), new Leaf(), new Leaf(), new Leaf(), new Leaf()
                }));                     //one branch with leafs
                plants.Add(new Leaf());  //one leaf
                foreach (IPlant plant in plants)
                {
                    plant.Eat();
                }
            }

            void AdapterCalling()
            {
                var bagOfPeelableFruit = new List <IPeelable>();

                bagOfPeelableFruit.Add(new Orange());
                bagOfPeelableFruit.Add(new Banana());
                bagOfPeelableFruit.Add(new SkinnableTOPelableAdapter(new Apple()));
                bagOfPeelableFruit.Add(new SkinnableTOPelableAdapter(new Pear()));
                foreach (var fruit in bagOfPeelableFruit)
                {
                    fruit.Peel();
                }
            }

            void DecoratorCalling()
            {
                IPizza pizza           = new Pizza();
                IPizza cheeseDecorator = new CheeseDecorator(pizza);
                IPizza tomatoDecorator = new TomatoDecorator(cheeseDecorator);
                IPizza onionDecorator  = new OnionDecorator(tomatoDecorator);

                Debug.WriteLine(tomatoDecorator.getPizzaType());
            }

            void PrototypeCalling()
            {
                Developer dev = new Developer();

                dev.Name = "Ann";
                dev.Role = "Team Leader";
                dev.PreferredLanguage = "C#";
                Developer devCopy = (Developer)dev.Clone();

                devCopy.Name = "Anna"; //Not mention Role and PreferredLanguage, it will copy above
                Debug.WriteLine(dev.GetDetails());
                Debug.WriteLine(devCopy.GetDetails());
                Typist typist = new Typist();

                typist.Name           = "Beta";
                typist.Role           = "Typist";
                typist.WordsPerMinute = 120;
                Typist typistCopy = (Typist)typist.Clone();

                typistCopy.Name           = "Betty";
                typistCopy.WordsPerMinute = 115;//Not mention Role, it will copy above
                Debug.WriteLine(typist.GetDetails());
                Debug.WriteLine(typistCopy.GetDetails());
            }

            void MediatorCalling()
            {
                ConcessionsMediator mediator = new ConcessionsMediator();

                NorthConcessionStand leftKitchen  = new NorthConcessionStand(mediator);
                SouthConcessionStand rightKitchen = new SouthConcessionStand(mediator);

                mediator.NorthConcessions = leftKitchen;
                mediator.SouthConcessions = rightKitchen;

                leftKitchen.Send("Can you send some popcorn?");
                rightKitchen.Send("Sure thing, Kenny's on his way.");

                rightKitchen.Send("Do you have any extra hot dogs?  We've had a rush on them over here.");
                leftKitchen.Send("Just a couple, we'll send Kenny back with them.");
            }
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            // Create the pizza object
            Pizza pizza = null;
            // Creating two bools used to ensure user doesn't give bad input
            bool test1 = false;
            bool test2 = false;

            while (!test1 && !test2)
            {
                test1 = false;
                test2 = false;
                Console.WriteLine("Choose type of pizza: Calzone (c), deep pan (d) or regular pizza (r)");
                string in1 = Console.ReadLine();

                Console.WriteLine("Choose size of pizza: Lunch (l), regular (r) or family (f)");
                string in2 = Console.ReadLine();

                if (in1 == "c")
                {
                    if (in2 == "l")
                    {
                        pizza = new CalzonePizza(Pizza.SizeType.Lunch);
                        test1 = true;
                        test2 = true;
                    }
                    else if (in2 == "r")
                    {
                        pizza = new CalzonePizza(Pizza.SizeType.Regular);
                        test1 = true;
                        test2 = true;
                    }
                    else if (in2 == "f")
                    {
                        pizza = new CalzonePizza(Pizza.SizeType.Family);
                        test1 = true;
                        test2 = true;
                    }
                    else
                    {
                        Console.WriteLine("Invalid input for size - try again!");
                    }
                }
                else if (in1 == "d")
                {
                    if (in2 == "l")
                    {
                        pizza = new DeepPanPizza(Pizza.SizeType.Lunch);
                        test1 = true;
                        test2 = true;
                    }
                    else if (in2 == "r")
                    {
                        pizza = new DeepPanPizza(Pizza.SizeType.Regular);
                        test1 = true;
                        test2 = true;
                    }
                    else if (in2 == "f")
                    {
                        pizza = new DeepPanPizza(Pizza.SizeType.Family);
                        test1 = true;
                        test2 = true;
                    }
                    else
                    {
                        Console.WriteLine("Invalid input for size - try again!");
                    }
                }
                else if (in1 == "r")
                {
                    if (in2 == "l")
                    {
                        pizza = new NormalPizza(Pizza.SizeType.Lunch);
                        test1 = true;
                        test2 = true;
                    }
                    else if (in2 == "r")
                    {
                        pizza = new NormalPizza(Pizza.SizeType.Regular);
                        test1 = true;
                        test2 = true;
                    }
                    else if (in2 == "f")
                    {
                        pizza = new NormalPizza(Pizza.SizeType.Family);
                        test1 = true;
                        test2 = true;
                    }
                    else
                    {
                        Console.WriteLine("Invalid input for size - try again!");
                    }
                }
                else
                {
                    Console.WriteLine("Invalid input for pizza type - try again!");
                }
            }

            test1 = false;
            while (!test1)
            {
                Console.WriteLine("Pick topping: Bell pepper (1), cheese (2), chili (3), dressing (4), garlic (5), ham (6), kebab (7), lettuce (8), onion (9), pepperoni (10) or pineapple (11)");
                switch (Console.ReadLine())
                {
                case "1":
                    pizza = new BellPepperDecorator(pizza);
                    test1 = true;
                    break;

                case "2":
                    pizza = new CheeseDecorator(pizza);
                    test1 = true;
                    break;

                case "3":
                    pizza = new ChilliDecorator(pizza);
                    test1 = true;
                    break;

                case "4":
                    pizza = new DressingDecorator(pizza);
                    test1 = true;
                    break;

                case "5":
                    pizza = new GarlicDecorator(pizza);
                    test1 = true;
                    break;

                case "6":
                    pizza = new HamDecorator(pizza);
                    test1 = true;
                    break;

                case "7":
                    pizza = new KebabDecorator(pizza);
                    test1 = true;
                    break;

                case "8":
                    pizza = new LettuceDecorator(pizza);
                    test1 = true;
                    break;

                case "9":
                    pizza = new OnionDecorator(pizza);
                    test1 = true;
                    break;

                case "10":
                    pizza = new PepperoniDecorator(pizza);
                    test1 = true;
                    break;

                case "11":
                    pizza = new PineappleDecorator(pizza);
                    test1 = true;
                    break;

                default:
                    Console.WriteLine("Invalid input for topping");
                    break;
                }
            }

            test2 = false;
            while (!test2)
            {
                Console.WriteLine("Pick another topping: Bell pepper (1), cheese (2), chili (3), dressing (4), garlic (5), ham (6), kebab (7), lettuce (8), onion (9), pepperoni (10) or pineapple (11)");
                switch (Console.ReadLine())
                {
                case "1":
                    pizza = new BellPepperDecorator(pizza);
                    test2 = true;
                    break;

                case "2":
                    pizza = new CheeseDecorator(pizza);
                    test2 = true;
                    break;

                case "3":
                    pizza = new ChilliDecorator(pizza);
                    test2 = true;
                    break;

                case "4":
                    pizza = new DressingDecorator(pizza);
                    test2 = true;
                    break;

                case "5":
                    pizza = new GarlicDecorator(pizza);
                    test2 = true;
                    break;

                case "6":
                    pizza = new HamDecorator(pizza);
                    test2 = true;
                    break;

                case "7":
                    pizza = new KebabDecorator(pizza);
                    test2 = true;
                    break;

                case "8":
                    pizza = new LettuceDecorator(pizza);
                    test2 = true;
                    break;

                case "9":
                    pizza = new OnionDecorator(pizza);
                    test2 = true;
                    break;

                case "10":
                    pizza = new PepperoniDecorator(pizza);
                    test2 = true;
                    break;

                case "11":
                    pizza = new PineappleDecorator(pizza);
                    test2 = true;
                    break;

                default:
                    Console.WriteLine("Invalid input for topping");
                    break;
                }
            }

            pizza.GetDescription();

            Console.WriteLine("Total price: " + pizza.GetPrice() + " kr\n");
        }