Command Stack

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.

  1. Create an ICommand interface with methods execute() and undo()
  2. All undoable/redoable actions should be specified in classes that implement the ICommand interface
  3. Before the main body of execute() make sure you have all the information needed to undo

Now we need to create a CommandStack:

  1. Create a CommandStack class
  2. Add the following methods to the CommandStack
    • void execute(ICommand)
    • boolean isUndable()
    • void undo()
    • boolean isRedoable()
    • redo()
  3. All commands need to be executed through the execute(ICommand) method of the command stack
  4. Undo/Redo is also performed through the command stack