Command Processor Pattern
It uses the Command Pattern. The Command Processor pattern illustrates more specifically how command objects are managed. A central component of the pattern, the command processor, takes care of all command objects. The command processor schedules the execution of commands, may store them for later undo, and may provide other services such as logging the sequence of commands for testing purposes. Each command object delegates the execution of its task to supplier components within the functional core of the application.
Structure
Participants
- The controller represents the interface of the application. It accepts requests and creates the corresponding command objects. The command objects are then delivered to the command processor for execution.
- The command processor manages command objects, schedules them and starts their execution. It is the key component that implements additional services related to the execution of commands. The command processor remains independent of specific commands because it only uses the abstract command interface.
- Rest is similar to Command Pattern
Example
public interface AbstractCommand { public void doit(); public void undo(); public String getName(); public CmdType getType(); } public enum CmdType { normal, no_undo } public class DeleteCommand implements AbstractCommand { .... } //Combines several successive commands using Composite pattern. can have addCmd and delCmd methods... public class MacroCommand implements AbstractCommand{ List<AbstractCommand> commandList; @Override public void doit() { //do all the commands in the list } @Override public String getName() { } @Override public CmdType getType() { } @Override public void undo() { //undo all commands in the list } public void terminate(){ //delete commands in the list } } //The controller takes the role of the client public class Controller { CommandProcessor cmdProc; public void delPressed(){ AbstractCommand delCmd = new DeleteCommand(); cmdProc.doCmd(delCmd); } } //The command processor receives command objects from the controller and takes the role of the invoker, executing command objects public class CommandProcessor { List<AbstractCommand> doneStack; public void doCmd(AbstractCommand cmd){ cmd.doit(); switch (cmd.getType()){ case normal: doneStack.add(cmd); case no_undo: doneStack.clear(); } } public void undoLastCmd(){ //from the donestack } public void redoLastCmd(){ //from the donestack } }
Related Patterns
Also See
page revision: 14, last edited: 31 Mar 2010 09:02