コード例 #1
0
        public void atenderDenuncias(IDenuncias denuncias)
        {
            Iterator.Iterator iter = denuncias.crearIterador();

            while (iter.hasNext())
            {
                IDenuncia actual = (IDenuncia)iter.Next();
                actual.atender(responsable);
            }
        }
コード例 #2
0
        /// <summary>
        /// 迭代器
        /// </summary>
        static void Iterator()
        {
            Console.WriteLine($"迭代器模式{Environment.NewLine}-------------------------------------------");

            Aggregate agg = new ConcreteAggregate();

            Iterator.Iterator iterator = agg.CreateIterator();

            while (iterator.Next())
            {
                Console.WriteLine(iterator.Current.ToString());
            }

            Console.ReadKey();
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: schangxiang/Code
        static void IteratorInvoke()
        {
            ConcreteAggregate a = new ConcreteAggregate();

            a[0] = "xiaoming";
            a[1] = "xiaohong";

            Iterator.Iterator i    = a.CreateIterator();
            object            item = i.First();

            while (!i.IsDone())
            {
                Console.WriteLine("{0}请Say Hi!", i.CurrentItem());
                i.Next();
            }
        }
コード例 #4
0
        static void Main(string[] args)
        {
            /**
             * ITERATOR
             **/

            ConcreteAggregate collection = new ConcreteAggregate();

            Iterator.Iterator iterator = collection.createIterator();

            //use to iterator to go through the collection
            while (!iterator.isDone())
            {
                Console.WriteLine(iterator.Next().ToString());
            }

            /**
             * OBSERVER
             **/

            Subject subject = new ConcreteSubject(SubjectState.Red);

            subject.Attach(new ConcreteObserver());
            subject.Attach(new ConcreteObserver());
            subject.Attach(new ConcreteObserver());

            ((ConcreteSubject)subject).SubjectState = SubjectState.Blue;

            /**
             * DECORATOR
             **/

            //we create a class with a base function
            Component component = new ConcreteComponent();

            //we create the function we want the add to the first class
            Decorator.Decorator decorator = new ConcreteDecorator();

            //the decorator gets the functionality of the class we want to extend
            decorator.SetBaseFuntionality(component);

            //both ops are called
            decorator.Operation();

            /**
             * COMMAND
             **/

            Command.Command command = new ConcreteCommand(new Receiver());

            //create an invoker which in turn calls the command which IN TURN calls the receiver
            Invoker invoker = new Invoker();

            invoker.SetCommand(command);
            invoker.ExecuteCommand();

            /**
             * COMPOSITE
             **/

            //instanciate differents persons
            Composite.Component component1 = new Composite.Composite("Bob");
            Composite.Component component2 = new Composite.Composite("Patrick");
            Composite.Component component3 = new Composite.Composite("Johny");
            Composite.Component component4 = new Composite.Composite("Pierre");

            //affects differents persons to a superior
            component1.Add(component2);
            component2.Add(component3);
            component2.Add(component4);

            //default method for every objects in the tree
            component1.Operation();

            /**
             * MEMENTO
             **/

            Originator originator = new Originator();
            CareTaker  careTaker  = new CareTaker();

            //create a couple of states
            originator.State = "state1";
            originator.State = "state2";
            //save at some point
            careTaker.Add(originator.SaveToMemento());
            originator.State = "state3";

            //restore state at the time of the save
            originator.RestoreStateFromMemento(careTaker.Get(0));

            Console.WriteLine(originator.State);

            Console.ReadKey();
        }
コード例 #5
0
        public Calculation EditCalculation(String operation, Calculation calculation, ICalculatorComponent calculator, Iterator.Iterator iterator)
        {
            bool end1 = operation.Equals("addition");
            bool end2 = operation.Equals("subtraction");
            bool end3 = operation.Equals("multiplication");
            bool end4 = operation.Equals("division");
            bool end5 = operation.Equals("square root");
            bool end6 = operation.Equals("square");

            while (end1)
            {
                Prompts._FirstNumber();
                double a = Convert.ToDouble(Console.ReadLine());
                Prompts._SecondNumber();
                double b = Convert.ToDouble(Console.ReadLine());

                calculation = _edit.TwoVariables(calculation, a, b, operation);
                iterator.SetTwoVariableCalculation(calculator, calculation, "+");

                end1 = false;
            }

            while (end2)
            {
                Prompts._FirstNumber();
                double a = Convert.ToDouble(Console.ReadLine());
                Prompts._SecondNumber();
                double b = Convert.ToDouble(Console.ReadLine());

                calculation = _edit.TwoVariables(calculation, a, b, operation);
                iterator.SetTwoVariableCalculation(calculator, calculation, "-");

                end2 = false;
            }

            while (end3)
            {
                Prompts._FirstNumber();
                double a = Convert.ToDouble(Console.ReadLine());
                Prompts._SecondNumber();
                double b = Convert.ToDouble(Console.ReadLine());

                calculation = _edit.TwoVariables(calculation, a, b, operation);
                iterator.SetTwoVariableCalculation(calculator, calculation, "*");

                end3 = false;
            }

            while (end4)
            {
                Prompts._FirstNumber();
                double a = Convert.ToDouble(Console.ReadLine());
                double b = 0;
                bool   c = true;

                while (c)
                {
                    Prompts._SecondNumber();
                    b = Convert.ToDouble(Console.ReadLine());

                    while (b == 0)
                    {
                        Prompts.Zero();
                        b = Convert.ToDouble(Console.ReadLine());
                    }

                    c = false;
                }

                calculation = _edit.TwoVariables(calculation, a, b, operation);
                iterator.SetTwoVariableCalculation(calculator, calculation, "/");

                end4 = false;
            }

            while (end5)
            {
                Prompts._Number();
                double a = Convert.ToDouble(Console.ReadLine());

                calculation = _edit.OneVariable(calculation, a, operation);
                iterator.SetOneVariableCalculation(calculator, calculation, "SQUARE ROOT OF");

                end5 = false;
            }

            while (end6)
            {
                Prompts._Number();
                double a = Convert.ToDouble(Console.ReadLine());

                calculation = _edit.OneVariable(calculation, a, operation);
                iterator.SetOneVariableCalculation(calculator, calculation, "SQUARE OF");

                end6 = false;
            }

            return(calculation);
        }
コード例 #6
0
        public static void Activate(ICalculatorComponent calculator, String userInput)
        {
            Collection collection = new Collection();

            foreach (Calculation _calculation in calculator.CalculationHistory)
            {
                collection.CalculationHistory.Add(_calculation);
            }

            Iterator.Iterator iterator = collection.CreateIterator();


            // | 1 | View Calculation History (Format: Entire List)
            while (userInput.Equals("1"))
            {
                Prompts.ShowHistory();
                iterator.ShowAll(calculator);
                Prompts.Back();
                String enter = Console.ReadLine();
                userInput = "pass";
            }

            // | 2 | View Calculation History (Format: One By One)
            while (userInput.Equals("2"))
            {
                String enter = "";

                Prompts.IteratorTitle();
                iterator.First(calculator);

                while (enter != "EXIT")
                {
                    Prompts.IteratorOptions();
                    enter = Console.ReadLine();

                    while (enter.Equals("NEXT"))
                    {
                        iterator.Next(calculator);
                        enter = "pass";
                    }

                    while (enter.Equals("PREVIOUS"))
                    {
                        iterator.Previous(calculator);
                        enter = "pass";
                    }

                    while (enter.Equals("FIRST"))
                    {
                        iterator.First(calculator);
                        enter = "pass";
                    }

                    while (enter.Equals("LAST"))
                    {
                        iterator.Last(calculator);
                        enter = "pass";
                    }

                    while (enter.Equals("CHANGE"))
                    {
                        Prompts.ShowEdit();
                        Receiver.DisplayOperations(calculator, "");
                        string operation = Console.ReadLine();

                        CalculationManipulation manipulator    = new CalculationManipulation();
                        Calculation             oldCalculation = iterator.CurrentCalculation(calculator);
                        manipulator.EditCalculation(operation, oldCalculation, calculator, iterator);

                        iterator.Current(calculator);
                        WriteToConsole.Write("");

                        Prompts.Changed();
                        string temp = Console.ReadLine();
                        enter = "EXIT";
                    }

                    while (enter.Equals("REMOVE"))
                    {
                        CalculationManipulation manipulator = new CalculationManipulation();
                        manipulator.RemoveCalculation(calculator, iterator.GetIndex());
                        enter = "pass";
                        Prompts.Removed();
                        string temp = Console.ReadLine();
                        enter = "EXIT";
                    }

                    while ((!enter.Equals("NEXT")) && (!enter.Equals("PREVIOUS")) && (!enter.Equals("FIRST")) && (!enter.Equals("LAST") && (!enter.Equals("CHANGE") && (!enter.Equals("REMOVE")) && (!enter.Equals("pass")) && (!enter.Equals("EXIT")))))
                    {
                        Prompts.Enter();
                        string temp = Console.ReadLine();
                        enter = "pass";
                    }
                }

                userInput = "pass";
            }

            // | 5 | Check State Of All Calculations
            while (userInput.Equals("5"))
            {
                Prompts.StateTitle();
                iterator.ShowState(calculator);
                Prompts.StateOptions();
                String enter = Console.ReadLine();
                userInput = "pass";
            }
        }