- Forward


The Command Pattern
An Introduction with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Overview
Back SMYC Forward
  • The "Problem":
    • Encapsulate a request/operation as an object
  • Examples:
    • Callbacks (i.e., a request/operation that is "registered" at one point in time and then "called back" later)
    • Logging requests/operations
    • Queueing requests/operations
    • Supporting requests/operations that can be "un-done" (i.e., the request/operation can store state information about the operand and then restore the operand's state)
A Model of the Command Pattern
Back SMYC Forward

In UML

command
Improving this Model
Back SMYC Forward
  • An Observation:
    • This solution is not very flexible
  • Adding Flexibility:
    • Make Operation an interface
    • Make Operand an interface
An Example
Back SMYC Forward
javaexamples/command/TextDocument.java
 
An Example (cont.)
Back SMYC Forward
javaexamples/command/Replace.java
 
An Example (cont.)
Back SMYC Forward
javaexamples/command/Driver.java
 
There's Always More to Learn
Back -