如何实现撤销功能?

发布于 2024-10-07 13:14:55 字数 461 浏览 3 评论 0原文

在我的应用程序中,我想为用户提供一个小的撤消功能。用户可以撤消的操作并不多。特别是这些操作是:

  • 向对象添加注释 为对象
  • 着色
  • 用字符串标记对象

现在我考虑如何实现它。我首先想到了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

锦上情书 2024-10-14 13:14:55

撤消/重做通常使用命令模式来实现。 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.

冰火雁神 2024-10-14 13:14:55

你可以做这样简单的事情:

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;
}

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;
}
挥剑断情 2024-10-14 13:14:55

您应该为要撤消的每个操作实现命令模式

如何在程序不发生重大改变的情况下实现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

单身狗的梦 2024-10-14 13:14:55

正确且经过验证的 UNDO 功能实现是命令模式

For Correct and proven implememtation for UNDO functionality is Command Pattern

半枫 2024-10-14 13:14:55

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文