Command stack is a design pattern if we need to support for undo/redo operations.
There are two aspects to this design pattern ICommand
interface where we can define the behaviour of the execution and undo operation and a CommandStack
which is a generic.
- Create an
ICommand
interface with methodsexecute()
andundo()
- All undoable/redoable actions should be specified in classes that implement the
ICommand
interface - Before the main body of
execute()
make sure you have all the information needed to undo
Now we need to create a CommandStack
:
- Create a
CommandStack
class - Add the following methods to the
CommandStack
void execute(ICommand)
boolean isUndable()
void undo()
boolean isRedoable()
redo()
- All commands need to be executed through the
execute(ICommand)
method of the command stack - Undo/Redo is also performed through the command stack