实现命令模式

发布于 2024-08-20 16:27:53 字数 194 浏览 4 评论 0原文

我正在设计一个应用程序,我想使用 命令模式 进行撤消/redo 目的。我对命令模式做了一些研究,但我唯一不明白的是:命令是否应该具有撤消和重做方法,或者我应该创建两个单独的命令,一个用于撤消,一个用于重做,并从主命令本身?

I am in the design process of an application, and I would like to use the command pattern for undo/redo purposes. I did some research into the command pattern but the only thing I don't get is: Should a command have the undo and redo methods, or should I make two separate commands, one for undo and one for redo, and call those from the main command itself?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

_失温 2024-08-27 16:27:53

命令对象本身应该实现撤消/重做功能。

命令通常从命令管理器维护的堆栈中压入和弹出,以实现多级撤消。当命令被执行时,它们被压入堆栈;当它们被撤消时,它们被从堆栈中弹出。

备忘录模式将与命令模式结合使用,它不能替代命令模式的使用。它将用于维护撤消操作所需的状态。

The command object itself should implement the undo / redo functionality.

The commands are generally pushed and popped from a stack maintained by a command manager to implement multi level undo. When commands are executed they are pushed onto the stack and when they are undone they are popped from the stack.

The memento pattern would be used in conjunction with the command pattern, it is not a replacement to the usage of the command pattern. It would be used to maintain the state required for the undo operation.

时光匆匆的小流年 2024-08-27 16:27:53

您可能还想考虑为此的纪念品模式,我们使用它并且它对于撤消非常有效。

You might also want to consider the memento pattern for this, we use it and it works brilliant for undo.

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