- Forward


Using the Command Pattern to Decentralize Control
An Introduction with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

The Setting
Back SMYC Forward
  • An Example:
    • A text editor with copy/cut/paste capabilities and a GUI with a menu, toolbar, and popup menu
  • A Noncohesive Design :
    • The editing capabilities, the GUI components, and the event handling are all in one class
A Noncohesive Design
Back SMYC Forward
TextEditor_NotCohesive
An Implementation of the Noncohesive Design
Back SMYC Forward
javaexamples/action/notcohesive/TextEditorApplication.java
 
Improving the Design
Back SMYC Forward
  • The Idea:
    • Split the editing functionality from the GUI functionality
  • The Result:
    • Control is centralized
A Design with Centralized Control
Back SMYC Forward
TextEditor_Centralized
An Implementation with Centralized Control
Back SMYC Forward
javaexamples/action/TextEditor.java
 
An Implementation with Centralized Control (cont.)
Back SMYC Forward
javaexamples/action/centralizedcontrol/TextEditorGUI.java
 
Decentralizing Control
Back SMYC Forward
  • The Idea:
    • Without the Command Pattern there is usually a central controller that observes all GUI components
    • Using the Command Pattern, each operation can control itself
  • Support in Java:
    • The Action interface
    • The AbstractAction class
A Design with Decentralized Control
Back SMYC Forward
command-pattern_actions
An Implementation with Decentralized Control (cont.)
Back SMYC Forward
javaexamples/action/EditorAction.java
 
An Implementation with Decentralized Control (cont.)
Back SMYC Forward
javaexamples/action/CopyAction.java
 
An Implementation with Decentralized Control (cont.)
Back SMYC Forward
javaexamples/action/CutAction.java
 
An Implementation with Decentralized Control (cont.)
Back SMYC Forward
javaexamples/action/PasteAction.java
 
An Implementation with Decentralized Control (cont.)
Back SMYC Forward
javaexamples/action/TextEditorGUI.java
 
There's Always More to Learn
Back -