示例#1
0
        static void Main(string[] args)
        {
            var invoker    = new Invoker();
            var calculator = new Calculator();

            var command1 = new CalculatorCommand(calculator, CalculatorCommand.Operation.Add, 10);
            var command2 = new CalculatorCommand(calculator, CalculatorCommand.Operation.Multiply, 5);
            var command3 = new CalculatorCommand(calculator, CalculatorCommand.Operation.Subtract, 5);
            var command4 = new CalculatorCommand(calculator, CalculatorCommand.Operation.Divide, 5);

            invoker.Command = command1;
            invoker.Invoke();

            invoker.Command = command2;
            invoker.Invoke();

            invoker.Command = command3;
            invoker.Invoke();
            invoker.Undo();

            invoker.Command = command4;
            invoker.Invoke();

            Console.WriteLine($"Result is : {calculator.Value}");
        }
        static void Main(string[] args)
        {
            Calculadora calculadora = new Calculadora();

            Invoker invoker = new Invoker();

            invoker.Compute(new Soma(calculadora, 10));
            invoker.Compute(new Soma(calculadora, 5));
            invoker.Compute(new Soma(calculadora, 10));
            //Desfazer as duas �ltimas opera��es
            invoker.Undo(2);


            Console.ReadKey();
        }
示例#3
0
        /// <summary>
        /// Метод меняет состояние самолёта
        /// </summary>
        /// <param name="plane">Самолёт</param>
        /// <param name="receiver">Исполнитель команд</param>
        /// <returns></returns>
        private static int PlaneControl(Airplane plane, Invoker receiver)
        {
            Console.WriteLine("Что делать самолёту? ");
            int a = 1;

            try
            {
                a = Convert.ToInt32(Console.ReadLine());
                if (a != 1 && a != 2 && a != 3 && a != 4)
                {
                    throw new Exception("Неверная команда");
                }
            }
            catch (FormatException)
            {
                Console.WriteLine("Введите команду правильно");
                return(0);
            }
            catch (Exception)
            {
                Console.WriteLine("Такой команды нет");
                return(0);
            }

            switch (a)
            {
            case 1:
            {
                receiver.Command = new TakeOff(plane);
                receiver.Run();
            }
                ; break;

            case 2:
            {
                receiver.Command = new Landing(plane);
                receiver.Run();
            }
                ; break;

            case 3:
                receiver.Undo();
                ; break;
            }
            Console.WriteLine(plane.State);
            return(a);
        }