In my application there is a multidimensional array with potentially a lot of data so completely saving that object every time the user changes it seems unfeasible.
I read about the Command pattern and eventually found this article but I do not quite understand it. I'm not sure about how his code example works and whether or not this will work in my application. Also, is this "new" method preferable to the GoF pattern?
My application has tools like a brush and fill tool that work on the current document and I'm not really sure how to best implement undo/redo functionality but I know saving the state of the object for every operation would not allow for infinite undo's and redo's which is what I'm after. I'm not sure if the Command pattern can be used in this context or whether or not and how the article's implementation works.
Hopefully someone can elaborate upon the article or perhaps explain how the Command pattern could be adapted to my needs. Thanks for reading!
Create a class with three values (location, oldvalue, and newvalue, where location points to an element in your multidimensional array) and two methods (undo, redo). The on each operation, creat an array of these object for each element in your large array that changes, and push it onto an undo stack. When popping off the undo stack, call undo, and then push to a redo stack. Opposite for redo pop. Just remember to clear the redo stack with new actions.
EDIT:
Sample:
public void undo()
{
location = oldvalue;
}
public void redo()
{
location = newvalue;
}
And then an example of the stacks:
command = undoStack.Pop();
command.undo();
redoStack.Push(command);