Hi I'm about to add new functionality to application which I'm currently writting. I need to write a undo/redo fnctionality. However 90% of our application is ready and I don't know what is the best way to implementing this functionality without affectig(too much ) code which has been already created.
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
As Reed Copsey says the most common pattern to implementing do/redo is Command Pattern. The basic idea is to implement actions as commands that implemets some interface like this:
Then You have a class (Control) that executes all the commands as a whole, such class must be composed by a group of commands, the when you execute the commands each command is pushed inside a Stack (by means the
push()
method), in the case you want to undo the actions then you take every element from the Stack (by means ofpop()
) an executes itsUndo()
method.Note: this is a very simple code, only for trying to clarify the ideas behind the Command Pattern.
link text
Couple of questions: What does the undo do? Does it undo all changes or only the last one?
In any case, you would first store the state of the input fields (initial state, or last desired position), then the undo button would take that state and apply it to the field.
Redo would be the same concept, but it would store the state previous to the undo.
You'll need an undo/redo stack, and major changes to make everything aware of this stack.. I'm sorry, but magic doesn't exist :/
There's a free library written by Rockford Lhotka for Business Objects that includes built in undo/redo functionality. I think he even implements it through an interface, so it should be "minimally invasive".
There aren't many details here. However, Undo/Redo functionality is typically handled via some variation of the Command Pattern. Depending on your architecture, this could be a simple reworking of your basic functionality into "commands", or a major overhaul.