问题描述
我需要为我的窗口应用程序(类似 powerpoint 的编辑器)实现撤消/重做框架,应该遵循的最佳实践是什么,如何处理我的对象的所有属性更改以及它在 UI 上的反映.
I need to implement Undo/Redo frame work for my window application(editor like powerpoint), what should be the best practice to follow, how would be handle all property changes of my objects and it reflection on UI.
推荐答案
有两种经典模式可以使用.第一个是 memento 模式,用于存储完整对象状态的快照.这可能比命令模式更加系统密集,但它允许非常简单地回滚到较旧的快照.您可以将快照存储在磁盘上,例如 PaintShop/PhotoShop,或者将它们保存在内存中以用于不需要持久性的较小对象.你所做的正是这个模式的设计目的,所以它应该比其他人建议的命令模式更符合要求.
There are two classic patterns to use. The first is the memento pattern which is used to store snapshots of your complete object state. This is perhaps more system intensive than the command pattern, but it allows rollback very simply to an older snapshot. You could store the snapshots on disk a la PaintShop/PhotoShop or keep them in memory for smaller objects that don't require persistence. What you're doing is exactly what this pattern was designed for, so it should fit the bill slightly better than the Command Pattern suggested by others.
另外,还有一点需要注意的是,因为它不需要您使用互惠命令来撤消之前完成的操作,这意味着任何可能的单向功能 [例如散列或加密] 都无法撤消只需回滚到较旧的快照,仍然可以非常简单地撤消使用互惠命令的琐碎操作.
Also, an additional note is that because it doesn't require you to have reciprocal commands to undo something that was previously done, it means that any potentially one way functions [such as hashing or encryption] which can't be undone trivially using reciprocal commands can still be undone very simply by just rolling back to an older snapshot.
正如所指出的,命令模式可能占用较少的资源,所以我会承认,在以下特定情况下:
Also as pointed out, the command pattern which is potentially less resource intensive, so I will concede that in specific cases where:
- 有一个大的对象状态需要持久化和/或
- 没有破坏性的方法和
- 可以非常简单地使用互惠命令来逆转所采取的任何行动
命令模式可能更合适[但不一定,这在很大程度上取决于具体情况].在其他情况下,我会使用备忘录模式.
the command pattern may be a better fit [but not necessarily, it will depend very much on the situation]. In other cases, I would use the memento pattern.
我可能会避免使用两者的混搭,因为我倾向于关心将在我身后出现并维护我的代码的开发人员,以及我对雇主的道德责任是让这个过程变得简单并且尽可能便宜.我看到这两种模式的混搭很容易变成一个无法维护的不适感,维护成本很高.
I would probably refrain from using a mashup of the two because I tend to care about the developer that's going to come in behind me and maintain my code as well as it being my ethical responsibility to my employer to make that process as simple and inexpensive as possible. I see a mashup of the two patterns easily becoming an unmaintainable rat hole of discomfort that would be expensive to maintain.
这篇关于Undo Redo 实施的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!