实现命令模式
我正在设计一个应用程序,我想使用 命令模式 进行撤消/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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
命令对象本身应该实现撤消/重做功能。
命令通常从命令管理器维护的堆栈中压入和弹出,以实现多级撤消。当命令被执行时,它们被压入堆栈;当它们被撤消时,它们被从堆栈中弹出。
备忘录模式将与命令模式结合使用,它不能替代命令模式的使用。它将用于维护撤消操作所需的状态。
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.
您可能还想考虑为此的纪念品模式,我们使用它并且它对于撤消非常有效。
You might also want to consider the memento pattern for this, we use it and it works brilliant for undo.