Question

So, I understand that undo/redo is usually implemented by command pattern. However, when a command is intend to repeat x times, then undo x times would be troublesome for the users.

For example, I have a "int num", when I press "+" on the keyboard, the program will do "++num". If the user increase the num from 0 to 50 by pressing "+", then the user want to undo, how do I allow the user to undo once, and the num will be back to 0.

How to implement undo so that it can handle a series of repeated commands?

Thanks in advance!

Was it helpful?

Solution

The Monitored Undo Framework ( http://muf.codeplex.com ) does this by using the concept of a Batch of operations. You can flag a set of operations as belonging to a group so that the undo system will undo / redo them as a unit of work.

Furthermore, the library allows you to optimize the situation by only storing the first / last values for a given field. That way, the undo / redo logic doesn't have to apply all 50 operations. It can simply undo by setting the value back to what it was prior to the undo batch.

Caveat: The MUF library doesn't use a traditional command pattern. It uses more of a memento pattern, tracking changes after they happen in the underlying domain model.

If you needed to have a true command pattern, then you might be able to add logic to the undo implementation that would inspect entries on the undo stack. Then, for example, if a user hits undo on the "+" operation, the stack would begin an undo, and keep undoing as long as it kept finding "+" operations on the stack. I've used this approach in cases where I couldn't batch the events, but wanted the undo stack to automatically undo more than one operation at a time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top