How to implement undo functionality?

2020-05-27 04:51发布

In my application I want to provide the user with a small undo functionality. There aren't many actions than can be undone by the user. Particularly the actions are:

  • Add notes to an object
  • Color an object
  • Tag a objcet with a string

Now I thought about how to implement this. I first thought of a Action Class that is the abstract base class for the 3 different actions that can be taken by the user. Every time the user takes on of these actions, a new appropriate instance of a subclass of this abstract Action class is created and inserted into a list that contains all actions.

Whenever the user wants to undo something, the list is displayed to the user and he can choose which action he want to undo.

Now I was thinking what has to be stored in such an action object:

  • the state of the object before the action
  • the actual action that was taken (e.g. the string that was added to a object's notes)

I'm not sure if this is enough. I also thought about something like a chronological ordering, but this should be necessary since the list can be maintained chronologically correct.

Are there any other things I should consider?

5条回答
闹够了就滚
2楼-- · 2020-05-27 04:58

Its hard to overlook this Simple-Undo-redo-library-for-Csharp-NET when adding Undo/Redo functionality to existing projects.

查看更多
够拽才男人
3楼-- · 2020-05-27 05:01

You could do something simple like this:

Stack<Action> undoStack = new Stack<Action>();    

void ChangeColor(Color color)
{
    var original = this.Object.Color;
    undoStack.Push(() => this.Object.Color = original);
    this.Object.Color = color;
}
查看更多
ゆ 、 Hurt°
4楼-- · 2020-05-27 05:01

For Correct and proven implememtation for UNDO functionality is Command Pattern

查看更多
SAY GOODBYE
5楼-- · 2020-05-27 05:14

you should implement the Command Pattern for every action you want undo:

how to implement undo/redo operation without major changes in program

查看更多
三岁会撩人
6楼-- · 2020-05-27 05:20

Undo/redo is commonly implemented with the Command Pattern. The Action class can be used as the basis for this, but you need a 'do' action and an 'undo' action within each command. Here is an example of this in practice. You should probably store the commands executed in a stack as it makes it much easier to implement and much easier for the user to follow.

查看更多
登录 后发表回答