快速撤消/重做与纪念品/命令模式? [英] Fast undo/redo with memento/command pattern?

查看:207
本文介绍了快速撤消/重做与纪念品/命令模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为手机编写一个绘画/图形Java应用程序(因此内存有限)。应用状态基本上是三个1000x500位图(即绘画层)。加载三个位图需要大约2或3秒。



我正在尝试编写一个撤消引擎,但是我无法找出一个很好的方法。典型的方法是:




  • 使用命令模式:撤消时,重新载入初始文件的状态,然后播放除了最后一个,所有处理的命令到目前为止。尽管如此,只要等待2或3秒才能加载太慢的初始状态。没有足够的内存来存储内存中的初始状态。


  • 使用memento模式:撤消时,您将替换当前状态的一部分那是随着旧状态而改变的。这意味着每个操作都需要将旧状态的位图保存到磁盘,因为移动设备上没有足够的内存将其存储在内存中。由于保存位图需要时间,如果用户决定使用例如油漆很多画笔笔画快速连续?我不能让他们等待。




我所有的解决方案都涉及上述模式的复杂混合。



任何人都可以提出一个解决方案,让我对我的应用程序有相当快速的撤消/重做?

解决方案

有一种处理撤消的第三种常见方法。那就是存储Undo对象内的两个状态之间的差异。您可以将其作为实际的差异(即哪些像素已经改变,它们改变了什么),但这可能几乎与在每个阶段存储位图一样浪费内存。或者,您可以使用命令模式方法,但是在撤消时不再重新运行命令,您可以存储命令的倒数 - 即如果用户增加了红色值为10,则undo命令将其减少10。要撤消,只需执行反向命令即可。一些命令很难找到一个反转,例如转换为黑白,而是通过将底层位图与许多通过命令打开或关闭的过滤器进行混合,您可以执行此操作。



作为另一个建议,使用您提到的命令方法,但保留上一步的位图。当用户执行撤消时,立即从先前(n-1)步骤中显示缓存的位图,然后开始计算n-2的位图,以便在他再次按下撤消时准备好。


I'm writing a painting/graphics Java application for a mobile phone (so memory is limited). The application state is essentially three 1000x500 bitmaps (i.e. layers of a painting). Loading three bitmaps takes about 2 or 3 seconds.

I'm trying to write an undo engine but I just cannot work out a good way to do it. The typical approaches are:

  • Use the command pattern: When you undo, you reload the state of the initial file and then playback all the commands processed so far except for the final one. Doing this naively though means waiting 2 or 3 seconds to load the initial state which is too slow. There isn't enough memory to store the initial state in memory either.

  • Use the memento pattern: When you undo, you replace the part of the current state that was changed with the old state. This means every action needs to save bitmaps of the old state to disk because there just isn't enough memory on a mobile device to store this in memory. As saving bitmaps takes time, how do I cope if the user decides to e.g. paint many brush strokes in quick succession? I cannot make them wait.

All my solutions involve complex hybrids of the above patterns.

Can anyone suggest a solution that would allow me to have reasonably fast undo/redo for my application?

解决方案

There is a third common method of handling undo. That is to store the differences between the two states within the Undo object. You can do this as actual differences (i.e. as which pixels have changed and what they changed to) but that is probably nearly as wasteful of memory as storing the bitmap at every stage.

Alternatively you can use the command pattern approach, but instead of re-running the commands when you undo, you store the inverse of the command - i.e. if the user increased the red value by ten, then the undo command is to decrease it by ten. To undo you simply execute the inverse command. Some commands are hard to find an inverse for, such as "convert to black and white" but by mixing an underlying bitmap with a number of filters which are turned on or off by command you can probably do it.

As yet another suggestion, use the command approach you mentioned but keep a bitmap for the previous step. When the user does undo immediately display the cached bitmap from the previous (n-1) step and then start calculating the bitmap for n-2 so that you are ready for when he presses undo again.

这篇关于快速撤消/重做与纪念品/命令模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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