|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | + |
| 7 | +namespace DoFactory.GangOfFour.Command.RealWorld |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// The 'Invoker' class |
| 11 | + /// </summary> |
| 12 | + class User |
| 13 | + { |
| 14 | + // Initializers |
| 15 | + private Calculator _calculator = new Calculator(); |
| 16 | + private List<Command> _commands = new List<Command>(); |
| 17 | + private int _current = 0; |
| 18 | + |
| 19 | + public void Redo(int levels) |
| 20 | + { |
| 21 | + Console.WriteLine("\n---- Redo {0} levels ", levels); |
| 22 | + // Perform redo operations |
| 23 | + for (int i = 0; i < levels; i++) |
| 24 | + { |
| 25 | + if (_current < _commands.Count - 1) |
| 26 | + { |
| 27 | + Command command = _commands[_current++]; |
| 28 | + command.Execute(); |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + public void Undo(int levels) |
| 34 | + { |
| 35 | + Console.WriteLine("\n---- Undo {0} levels ", levels); |
| 36 | + // Perform undo operations |
| 37 | + for (int i = 0; i < levels; i++) |
| 38 | + { |
| 39 | + if (_current > 0) |
| 40 | + { |
| 41 | + Command command = _commands[--_current] as Command; |
| 42 | + command.UnExecute(); |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + public void Compute(char @operator, int operand) |
| 48 | + { |
| 49 | + // Create command operation and execute it |
| 50 | + Command command = new CalculatorCommand( |
| 51 | + _calculator, @operator, operand); |
| 52 | + command.Execute(); |
| 53 | + |
| 54 | + // Add command to undo list |
| 55 | + _commands.Add(command); |
| 56 | + _current++; |
| 57 | + } |
| 58 | + } |
| 59 | +} |
0 commit comments