Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- namespace TodoApplication
- {
- public interface ICom<TItem>
- {
- void Do(List<TItem> Items);
- void Undo(List<TItem> Items);
- }
- public class Adding<TItem> : ICom<TItem>
- {
- private TItem items;
- public Adding(TItem item)
- {
- item = items;
- }
- public void Do(List<TItem> Items)
- {
- Items.Add(Items);
- }
- public void Undo(List<TItem> Items)
- {
- Items.Remove(Items);
- }
- }
- public class Removing<TItem> : ICom<TItem>
- {
- private TItem items;
- private int index;
- public Removing(TItem item, int ind)
- {
- items = item;
- index = ind;
- }
- public void Do(List<TItem> Items)
- {
- Items.Add(items);
- }
- public void Undo(List<TItem> Items)
- {
- Items.RemoveAt(index);
- }
- }
- public class ListModel<TItem>
- {
- public enum Action
- {
- Pop,
- Push
- }
- private class HistoryUnit
- {
- public Action RealAction;
- public TItem Item;
- public int Index;
- }
- public List<TItem> Items;
- public int Limit;
- private LimitedSizeStack<HistoryUnit> actionHistory;
- public ListModel(int limit)
- {
- Items = new List<TItem>();
- Limit = limit;
- actionHistory = new LimitedSizeStack<HistoryUnit>(limit);
- }
- public void AddItem(TItem item)
- {
- var command = new HistoryUnit { RealAction = Action.Push, Item = item, Index = Items.Count };
- actionHistory.Push(command);
- Items.Add(item);
- }
- public void RemoveItem(int index)
- {
- var command = new HistoryUnit { RealAction = Action.Pop, Item = Items[index], Index = index };
- actionHistory.Push(command);
- Items.RemoveAt(index);
- }
- public bool CanUndo()
- {
- return actionHistory.Count != 0;
- }
- public void Undo()
- {
- var command = actionHistory.Pop();
- if (command.RealAction == Action.Pop)
- Items.Insert(command.Index, command.Item);
- else
- Items.RemoveAt(command.Index);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement