如何实现撤销功能?
在我的应用程序中,我想为用户提供一个小的撤消功能。用户可以撤消的操作并不多。特别是这些操作是:
- 向对象添加注释 为对象
- 着色
- 用字符串标记对象
现在我考虑如何实现它。我首先想到了一个 Action 类,它是用户可以执行的 3 种不同操作的抽象基类。每次用户执行这些操作时,都会创建此抽象 Action 类的子类的一个新的适当实例,并将其插入到包含所有操作的列表中。
每当用户想要撤消某些操作时,就会向用户显示该列表,用户可以选择要撤消的操作。
现在我在想什么必须存储在这样的操作对象中:
- 操作之前对象的状态
- 所采取的实际操作(例如添加到对象注释中的字符串)
我不确定这是否足够。我还考虑过按时间顺序排序之类的东西,但这应该是必要的,因为列表可以按时间顺序保持正确。
我还应该考虑其他事情吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
撤消/重做通常使用命令模式来实现。
Action
类可以用作此操作的基础,但每个命令中都需要一个“do”操作和一个“undo”操作。 以下是一个示例实践。您可能应该将执行的命令存储在堆栈中,因为它更容易实现并且用户更容易遵循。
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.
你可以做这样简单的事情:
You could do something simple like this:
您应该为要撤消的每个操作实现命令模式:
如何在程序不发生重大改变的情况下实现undo/redo操作
you should implement the Command Pattern for every action you want undo:
how to implement undo/redo operation without major changes in program
正确且经过验证的 UNDO 功能实现是命令模式
For Correct and proven implememtation for UNDO functionality is Command Pattern
很难忽视这个 Simple-Undo-redo-当向现有项目添加撤消/重做功能时,Csharp-NET 库。
Its hard to overlook this Simple-Undo-redo-library-for-Csharp-NET when adding Undo/Redo functionality to existing projects.