示例#1
0
        static void Main(string[] args)
        {
            // #region AbstractFactory

            // AbstractFactory factory1 = new ConcreteFactory1();

            // Client client1 = new Client(factory1);

            // client1.Run();

            // AbstractFactory factory2 = new ConcreteFactory2();

            // Client client2 = new Client(factory2);

            // client2.Run();

            // #endregion

            // #region FactoryMethod

            // // Note: constructors call Factory Method
            // DocumentFactory[] documents = new DocumentFactory[2];

            // documents[0] = new ConcreteResume();
            // documents[1] = new ConcreteReport();

            // foreach (var document in documents)
            // {
            //     Console.WriteLine(Environment.NewLine + document.GetType().Name + " -- ");

            //     foreach (var page in document.Pages)
            //     {
            //         Console.WriteLine(page.GetType().Name);
            //     }
            // }

            // #endregion

            /*   #region Adapter
             *
             * ITarget target = new Target();
             *
             * target.Requests();
             *
             * ITarget target1 = new Adapter();
             *
             * target1.Requests();
             *
             #endregion   */

            /* #region Singleton
             * Singleton singlone = Singleton.Instance();
             * Singleton singletwo = Singleton.Instance();
             *
             * System.Console.WriteLine("SingleOne {0}: ", singlone.ID);
             * System.Console.WriteLine("SingleOne {0}: ", singletwo.ID);
             #endregion             */

            /*#region Facade
             *
             * Computer facade = new Computer();
             *
             * facade.StartComputer();
             *
             #endregion*/

            /*#region Command
             *
             * IReceive receive = new Receive();
             *
             * ICommand commandOne = new InvokeOneCommand(receive);
             * ICommand commandTwo = new InvokeTwoCommand(receive);
             *
             * commandOne.Execute();
             * commandTwo.Execute();
             *
             #endregion*/

            // #region Strategy

            // var strategyA = new Context(new StrategyA());

            // strategyA.ConcretInterface();

            // var strategyB = new Context(new StrategyB());

            // strategyB.ConcretInterface();

            // #endregion

            // #region Observer

            // Subject subject = new Subject(new List<IObserver>());

            // IObserver observer1 = new Observer("Observer 1", subject);
            // IObserver observer2 = new Observer("Observer 2", subject);
            // IObserver observer3 = new Observer("Observer 3", subject);

            // subject.Subscribe(observer1);
            // subject.Subscribe(observer2);
            // subject.Subscribe(observer3);

            // subject.EditEdition();

            // Console.WriteLine();

            // subject.EditEdition();

            // Console.WriteLine();

            // subject.UnSubscribe(observer2);

            // Console.WriteLine();

            // subject.EditEdition();

            // #endregion

            #region Decorator

            IPizzaComponent pizza  = new PizzaComponent();
            CheeseDecorator chesse = new CheeseDecorator(pizza);
            TomateDecorator tomate = new TomateDecorator(chesse);

            Console.WriteLine($"Decorator Pattern Pizza: {tomate.GetDescription()} - {tomate.CalculateCost()}");
            #endregion

            Console.ReadKey();
        }
示例#2
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.");
            }
        }
示例#3
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");
        }