Command

The command pattern is a design pattern in which an object is used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.

Three terms always associated with the command pattern are client , invoker and receiver . The client instantiates the command object and provides the information required to call the method at a later time. The invoker decides when the method should be called. The receiver is an instance of the class that contains the method's code.

Using command objects makes it easier to construct general components that need to delegate, sequence or execute method calls at a time of their choosing without the need to know the owner of the method or the method parameters.

Structure

Image:Command_Design_Pattern_Class_Diagram.png

 

Example

/*the Invoker class*/
public class Switch {
 
    private Command flipUpCommand;
    private Command flipDownCommand;
 
    public Switch(Command flipUpCmd,Command flipDownCmd){
         this.flipUpCommand=flipUpCmd;
         this.flipDownCommand=flipDownCmd;
    }
 
    public void flipUp(){
         flipUpCommand.execute();
    }
 
    public void flipDown(){
         flipDownCommand.execute();
    }
 
}
 
 
/*Receiver class*/
 
public class Light{
 
     public Light(){  }
 
     public void turnOn(){
        System.out.println("The light is on");
     }
 
     public void turnOff(){
        System.out.println("The light is off");
     }
 
}
 
/*the Command interface*/
 
public interface Command{
    void execute();
}
 
 
/*the Command for turning on the light*/
 
public class FlipUpCommand implements Command{
 
   private Light theLight;
 
   public FlipUpCommand(Light light){
        this.theLight=light;
       }
 
   public void execute(){
      theLight.turnOn();
   }
 
}
 
 
 
/*the Command for turning off the light*/
 
public class FlipDownCommand implements Command{
 
   private Light theLight;
 
   public FlipDownCommand(Light light){
        this.theLight=light;
       }
 
   public void execute(){
      theLight.turnOff();
   }
 
}
 
/*The test class or client*/
public class PressSwitch{
 
   public static void main(String[] args){
       Light lamp = new Light();
       Command switchUp=new FlipUpCommand(lamp );
       Command switchDown=new FlipDownCommand(lamp );
 
       Switch s=new Switch(switchUp,switchDown);
 
       try {
           if (args[0].equalsIgnoreCase("ON")){
                s.flipUp();
           } else if (args[0].equalsIgnoreCase("OFF")){
               s.flipDown();
           } else{
               System.out.println("Argument /"ON/" or /"OFF/" is required.");
             }
           } catch (Exception e){
           System.out.println("Argument's required.");
       }
   }
}

Uses

Command objects are useful for implementing:

Multi-level undo 
If all user actions in a program are implemented as command objects, the program can keep a stack of the most recently executed commands. When the user wants to undo a command, the program simply pops the most recent command object and executes its undo() method.
Transactional behavior 
Undo is perhaps even more essential when it's called rollback and happens automatically when an operation fails partway through. Installers need this and so do databases. Command objects can also be used to implement two-phase commit .
Progress bars 
Suppose a program has a sequence of commands that it executes in order. If each command object has a getEstimatedDuration() method, the program can easily estimate the total duration. It can show a progress bar that meaningfully reflects how close the program is to completing all the tasks.
Wizards 
Often a wizard presents several pages of configuration for a single action that happens only when the user clicks the "Finish" button on the last page. In these cases, a natural way to separate user interface code from application code is to implement the wizard using a command object. The command object is created when the wizard is first displayed. Each wizard page stores its GUI changes in the command object, so the object is populated as the user progresses. "Finish" simply triggers a call to execute() . This way, the command class contains no user interface code.
GUI buttons and menu items 
In Swing and Borland Delphi programming, an Action is a command object. In addition to the ability to perform the desired command, an Action may have an associated icon, keyboard shortcut, tooltip text, and so on. A toolbar button or menu item component may be completely initialized using only the Action object.
Thread pools  
A typical, general-purpose thread pool class might have a public addTask() method that adds a work item to an internal queue of tasks waiting to be done. It maintains a pool of threads that execute commands from the queue. The items in the queue are command objects. Typically these objects implement a common interface such as java.lang.Runnable that allows the thread pool to execute the command even though the thread pool class itself was written without any knowledge of the specific tasks for which it would be used.
Macro recording 
If all user actions are represented by command objects, a program can record a sequence of actions simply by keeping a list of the command objects as they are executed. It can then "play back" the same actions by executing the same command objects again in sequence. If the program embeds a scripting engine, each command object can implement a toScript() method, and user actions can then be easily recorded as scripts.
Networking 
It is possible to send whole command objects across the network to be executed on the other machines, for example player actions in computer games.
Parallel Processing 
Where the commands are written as tasks to a shared resource and executed by many threads in parallel (possibly on remote machines -this variant is often referred to as the Master/Worker pattern)
Mobile Code 
Using languages such as Java where code can be streamed/slurped from one location to another via URLClassloaders and Codebases the commands can enable new behavior to be delivered to remote locations (EJB Command, Master Worker)



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章