Browse Java Design Patterns & Enterprise Application Architecture

Implementing Undo and Redo with the Command Pattern

Use Command for undo and redo in Java when reversible intent and command history are worth modeling explicitly.

Undo and redo are where Command often proves its value.

What Must Be Modeled

Undoable command designs need:

  • reversible intent
  • enough prior state to undo correctly
  • a history stack
  • clear redo invalidation rules

Java Design Rule

Do not assume every command is naturally undoable. Some operations are irreversible or too expensive to snapshot cleanly. In those cases, forcing undo support into the same command contract can mislead the design.

Review Questions

  • What state must be captured before execution?
  • Is reversal deterministic?
  • When should redo history be cleared?

Command works well for undo only when reversal semantics are real and explicit.

Revised on Thursday, April 23, 2026