- Forward


The Command Pattern for Undo/Redo
An Introduction with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Overview
Back SMYC Forward
  • The Command Pattern can be used to Decentralize Control:
    • In the process, one can also add undo/redo support
  • Support in Java:
    • The AbstractUndoableEdit class encapsulates simple edits
    • The CompoundEdit class encapsulates more complicated edits
    • The UndoableEditListener interface describes the capabilities of observers
    • The UndoableEditSupport class helps manage observers
The Process
Back SMYC Forward
  • The Classes:
    • Create a class that encapsulates the edit
    • Create an Action class that implements javax.swing.undo.UndoableEditListener and handles actionPerformed() messages as desired
  • The Objects:
    • Create an instance of the Action
    • Associate it with a GUI component
    • Construct an UndoableEditSupport object
    • Add the Action to the UndoableEditSupport
    • Use the UndoableEditSupport to post the edit
An Example: A Football Stadium Scoreboard
Back SMYC Forward
  • Changes are difficult to make:
    • Involve many attributes
  • Often need to be:
    • Undone (e.g., when a penalty is called)
    • Redone (e.g., when a penalty is declined)
An Example (cont.)
Back SMYC Forward
javaexamples/undoredo/YardMarker.java
 
An Example (cont.)
Back SMYC Forward
javaexamples/undoredo/YardMarkerEdit.java
 
An Example (cont.)
Back SMYC Forward
Using Actions for Undo/Redo
javaexamples/undoredo/UndoRedoAction.java
 
An Example (cont.)
Back SMYC Forward
A Driver
javaexamples/undoredo/Driver.java
 
There's Always More to Learn
Back -