In my application I want to provide the user with a small undo functionality. There aren't many actions than can be undone by the user. Particularly the actions are:
- Add notes to an object
- Color an object
- Tag a objcet with a string
Now I thought about how to implement this. I first thought of a Action Class that is the abstract base class for the 3 different actions that can be taken by the user. Every time the user takes on of these actions, a new appropriate instance of a subclass of this abstract Action class is created and inserted into a list that contains all actions.
Whenever the user wants to undo something, the list is displayed to the user and he can choose which action he want to undo.
Now I was thinking what has to be stored in such an action object:
- the state of the object before the action
- the actual action that was taken (e.g. the string that was added to a object's notes)
I'm not sure if this is enough. I also thought about something like a chronological ordering, but this should be necessary since the list can be maintained chronologically correct.
Are there any other things I should consider?
Its hard to overlook this Simple-Undo-redo-library-for-Csharp-NET when adding Undo/Redo functionality to existing projects.
You could do something simple like this:
For Correct and proven implememtation for UNDO functionality is Command Pattern
you should implement the Command Pattern for every action you want undo:
how to implement undo/redo operation without major changes in program
Undo/redo is commonly implemented with the Command Pattern. The
Action
class can be used as the basis for this, but you need a 'do' action and an 'undo' action within each command. Here is an example of this in practice. You should probably store the commands executed in a stack as it makes it much easier to implement and much easier for the user to follow.