Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. private void SomeActionLogic()
  2. {
  3. MyAction action = new MyAction(parameter);
  4. actionManager.RecordAction(action);
  5. }
  6.  
  7. actionManager.Undo();
  8. ...
  9. actionManager.Redo();
  10.  
  11. class Program
  12. {
  13. static void Main(string[] args)
  14. {
  15. Console.WriteLine("Original color");
  16.  
  17. SetConsoleColor(ConsoleColor.Green);
  18. Console.WriteLine("New color");
  19.  
  20. actionManager.Undo();
  21. Console.WriteLine("Old color again");
  22.  
  23. using (Transaction.Create(actionManager))
  24. {
  25. SetConsoleColor(ConsoleColor.Red); // you never see Red
  26. Console.WriteLine("Still didn't change to Red because of lazy evaluation");
  27. SetConsoleColor(ConsoleColor.Blue);
  28. }
  29.  
  30. Console.WriteLine("Changed two colors at once");
  31.  
  32. actionManager.Undo();
  33. Console.WriteLine("Back to original");
  34.  
  35. actionManager.Redo();
  36. Console.WriteLine("Blue again");
  37. Console.ReadKey();
  38. }
  39.  
  40. static void SetConsoleColor(ConsoleColor color)
  41. {
  42. SetConsoleColorAction action = new SetConsoleColorAction(color);
  43. actionManager.RecordAction(action);
  44. }
  45.  
  46. static ActionManager actionManager = new ActionManager();
  47. }
  48.  
  49. class SetConsoleColorAction : AbstractAction
  50. {
  51. public SetConsoleColorAction(ConsoleColor newColor)
  52. {
  53. color = newColor;
  54. }
  55.  
  56. ConsoleColor color;
  57. ConsoleColor oldColor;
  58.  
  59. protected override void ExecuteCore()
  60. {
  61. oldColor = Console.ForegroundColor;
  62. Console.ForegroundColor = color;
  63. }
  64.  
  65. protected override void UnExecuteCore()
  66. {
  67. Console.ForegroundColor = oldColor;
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement