Quick undo / redo using a memory / command pattern?

I am writing a Java drawing / graphics application for a mobile phone (so that memory is limited). An application state is essentially three 1000x500 bitmaps (i.e. image layers). Downloading three bitmaps takes about 2 or 3 seconds.

I am trying to write a cancellation engine, but I just can't find a good way to do this. Typical approaches are:

  • Use the command template: when canceling, you reload the state of the source file and then play all the commands processed so far, except for the last. Doing this is naive, although it means waiting 2 or 3 seconds to load the initial state, which is too slow. There is not enough memory to store the initial state in memory.

  • Use a memory template: when canceling, you replace part of the current state that was changed using the old state. This means that each action must save raster images of the old state to disk, because there is not enough memory on the mobile device to store this in memory. Since saving bitmaps takes time, how can I handle it if the user decides, for example, to paint many brush strokes in quick succession? I can not make them wait.

All my decisions are connected with complex hybrids of the above patterns.

Can someone suggest a solution that would allow me to reasonably quickly undo / redo for my application?

+6
java optimization android oop design-patterns
source share
2 answers

There is a third common way to cancel processing. That is, store the differences between the two states in the Undo object. You can make it like real differences (that is, which pixels have changed and what they have changed), but it is probably almost as wasteful as storing a bitmap at each stage.

Alternatively, you can use the command template approach, but instead of restarting the commands when canceling, you save the inversion of the command - that is, if the user increased the red value by ten, the cancel command should decrease by ten. To cancel, you simply execute the reverse command. It is difficult for some teams to find an inversion, for example, "convert to black and white", but by mixing the base bitmap with several filters that are turned on or off by the command, you can do this.

As another suggestion, use the above command approach, but save the bitmap for the previous step. When the user cancels the immediate display of the cached bitmap from the previous (n-1) step, then start calculating the bitmap for n-2 so that you are ready when it depresses the cancel again.

+7
source share

About yours Use the command line template : starting from the initial state and again executing commands is not required at all. Each Command class should be a small user action and should have a mechanism to undo what it does in its execute () method if the undo operation should be supported. We keep a stack of *** Command objects. When the user cancels something, a Command object is issued from the stack and its undo () method is called.

I do not see any motivation for using the memento template in your case, since the cancellation actions will be in FIFO order. I suppose the user is not allowed to undo actions as he pleases.

+2
source share

All Articles