如何实现撤消功能? [英] How to implement undo functionality?

查看:174
本文介绍了如何实现撤消功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我想提供一个小的撤销功能的用户。有不超过可以由用户撤消许多动作。特别的行动是:

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:

  • 添加注释对象
  • 在颜色对象
  • 标签与字符串
  • 系统objcet
  • Add notes to an object
  • Color an object
  • Tag a objcet with a string

现在我想到了如何实现这一点。我首先想到的一个Action类是用于可采取由用户在3种不同的动作的抽象基类。用户采取这些行动的每一次,这个抽象的Action类的子类的一个新的合适的实例被创建并插入一个包含所有操作的列表。

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:

  • 对象的动作前的状态
  • 已采取的实际动作(例如,已添加到一个对象的纸币的字符串)

我不知道这是否是足够的。我也想过像一个时间顺序,而这应该是必要的,因为该列表可以按时间顺序保持正确的。

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?

推荐答案

撤销/重做通常与命令模式实施。该动作类可以作为依据的,但你需要一个做的动作,每个命令中的撤消操作。 下面是这在实践中的一个例子。 你应该存储在堆栈执行的命令,因为这使得它更容易实现和更加容易为用户遵循。

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.

这篇关于如何实现撤消功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆