撤消引擎的设计模式 [英] Design Pattern for Undo Engine

查看:95
本文介绍了撤消引擎的设计模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为民用工程应用程序编写结构建模工具。我有一个代表整个建筑物的巨大的模型类,其中包括节点,线元素,加载等的集合,它们也是自定义类。



我已经编写了一个撤消引擎,在每次修改模型后都会保存深层拷贝。现在我开始想,如果我可以有不同的编码。我可以使用相应的反向修饰符保存每个修饰符操作的列表,而不是保存深层副本。所以我可以将反向修饰​​符应用于当前模型进行撤消,或者修改器重做。



我可以想象如何执行改变对象属性的简单命令等。但复杂命令如何?像将新节点对象插入到模型中,并添加一些不断引用新节点的行对象。



如何实现? $ b

解决方案

我看过的大多数例子都使用了命令模式。每个可撤销的用户操作都可以获得自己的命令实例,其中包含所有信息以执行操作并将其回滚。然后,您可以保留已执行的所有命令的列表,然后可以逐个回滚。


I'm writing a structural modeling tool for a civil enginering application. I have one huge model class representing the entire building, which include collections of nodes, line elements, loads, etc. which are also custom classes.

I have already coded an undo engine which saves a deep-copy after each modification to the model. Now I started thinking if I could have coded differently. Instead of saving the deep-copies, I could perhaps save a list of each modifier action with a corresponding reverse modifier. So that I could apply the reverse modifiers to the current model to undo, or the modifiers to redo.

I can imagine how you would carry out simple commands that change object properties, etc. But how about complex commands? Like inserting new node objects to the model and adding some line objects which keep references to the new nodes.

How would one go about implementing that?

解决方案

Most examples I've seen use a variant of the Command-Pattern for this. Every user-action that's undoable gets its own command instance with all the information to execute the action and roll it back. You can then maintain a list of all the commands that have been executed and you can roll them back one by one.

这篇关于撤消引擎的设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆