Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. using System.Collections.Generic;
  2.  
  3. namespace TodoApplication
  4. {
  5.  
  6. public interface ICom<TItem>
  7. {
  8. void Do(List<TItem> Items);
  9. void Undo(List<TItem> Items);
  10. }
  11.  
  12. public class Adding<TItem> : ICom<TItem>
  13. {
  14. private TItem items;
  15.  
  16. public Adding(TItem item)
  17. {
  18. item = items;
  19. }
  20.  
  21. public void Do(List<TItem> Items)
  22. {
  23. Items.Add(Items);
  24. }
  25.  
  26. public void Undo(List<TItem> Items)
  27. {
  28. Items.Remove(Items);
  29. }
  30.  
  31.  
  32. }
  33.  
  34. public class Removing<TItem> : ICom<TItem>
  35. {
  36. private TItem items;
  37. private int index;
  38.  
  39. public Removing(TItem item, int ind)
  40. {
  41. items = item;
  42. index = ind;
  43. }
  44.  
  45. public void Do(List<TItem> Items)
  46. {
  47. Items.Add(items);
  48. }
  49.  
  50. public void Undo(List<TItem> Items)
  51. {
  52. Items.RemoveAt(index);
  53. }
  54. }
  55. public class ListModel<TItem>
  56. {
  57. public enum Action
  58. {
  59. Pop,
  60. Push
  61. }
  62.  
  63. private class HistoryUnit
  64. {
  65. public Action RealAction;
  66. public TItem Item;
  67. public int Index;
  68. }
  69.  
  70. public List<TItem> Items;
  71. public int Limit;
  72. private LimitedSizeStack<HistoryUnit> actionHistory;
  73. public ListModel(int limit)
  74. {
  75. Items = new List<TItem>();
  76. Limit = limit;
  77. actionHistory = new LimitedSizeStack<HistoryUnit>(limit);
  78. }
  79.  
  80. public void AddItem(TItem item)
  81. {
  82. var command = new HistoryUnit { RealAction = Action.Push, Item = item, Index = Items.Count };
  83. actionHistory.Push(command);
  84. Items.Add(item);
  85. }
  86.  
  87. public void RemoveItem(int index)
  88. {
  89. var command = new HistoryUnit { RealAction = Action.Pop, Item = Items[index], Index = index };
  90. actionHistory.Push(command);
  91. Items.RemoveAt(index);
  92. }
  93.  
  94. public bool CanUndo()
  95. {
  96. return actionHistory.Count != 0;
  97. }
  98.  
  99. public void Undo()
  100. {
  101. var command = actionHistory.Pop();
  102. if (command.RealAction == Action.Pop)
  103. Items.Insert(command.Index, command.Item);
  104. else
  105. Items.RemoveAt(command.Index);
  106. }
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement