用于实现UNDO和REDO选项的数据结构
我想实现 UNDO 和 REDO 选项(正如我们在 MS Word 等中看到的那样)。 你能建议我一个它的数据结构吗?我该如何实现它?
I want to implement UNDO and REDO option(as we see in MS word etc). Can you suggest me a data structure for it, and how can i implement it.?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
它不是一种数据结构,而是一种设计模式。 您正在寻找命令模式。
标准是将 Command 对象保留在堆栈中以支持多级撤消。 为了支持重做,第二个堆栈保留您已撤消的所有命令。 因此,当您弹出撤消堆栈以撤消命令时,您会将弹出的同一命令推送到重做堆栈中。 当您重做命令时,您会执行相反的操作。 您弹出重做堆栈并将弹出的命令推回撤消堆栈。
It isn't a data structure but a design pattern. You're looking for the Command Pattern.
The standard is to keep the Command objects in a stack to support multi level undo. In order to support redo, a second stack keeps all the commands you've Undone. So when you pop the undo stack to undo a command, you push the same command you popped into the redo stack. You do the same thing in reverse when you redo a command. You pop the redo stack and push the popped command back into the undo stack.
实际上,此功能(甚至四人帮)的标准模式是 Memento。
此外,虽然大多数程序都使用撤消/重做堆栈,但某些文本编辑器的爱好者更喜欢撤消/重做树,这样如果他们撤消一些命令,尝试一个新命令,就不会丢失整个历史记录,并改变他们的想法。
Actually, the standard pattern for this functionality (Gang of Four, even) is Memento.
Also, while most programs use Undo/Redo stacks, afficionados of certain text editors prefer Undo/Redo trees so that they don't lose their entire history if they undo a few commands, try a new one, and change their minds.
Objective-C Cocoa 有一个详细记录的 anwser,名为 NSUndoManager。
Objective-C Cocoa has a well documented anwser named NSUndoManager.
您可以使用命令模式来实现撤消/重做
检查这些示例:
http://www.codeproject。 com/csharp/undoredobuffer.asp
http://www.dofactory.com/Patterns /PatternCommand.aspx
You can use Command Pattern to achive Undo/Redo
Check these samples:
http://www.codeproject.com/csharp/undoredobuffer.asp
http://www.dofactory.com/Patterns/PatternCommand.aspx
这是命令模式的经典案例。 以下是 Python 中撤消功能的示例实现:
This is a classic case of Command Pattern. Following is a sample implementation of undo feature in Python :