Java Design Pattern: Command

Command design pattern takes an operation and its arguments and wraps them up in an object to be executed, logged, etc. In the example below, Command is an operation, its argument is a Computer, and they are wrapped in Switch.

In another perspective, Command pattern has 4 parts: command, receiver, invoker and client. In this example, Switch is the invoker, and Computer is the receiver. A concrete Command has a receiver object and invoke the receiver's method. Invoker can use different concrete command. The client determines which command to use for the receiver.

Command Pattern Class Diagram

command-design-pattern

Java Command Pattern Example

package designpatterns.command;
 
import java.util.List;
import java.util.ArrayList;
 
/* The Command interface */
interface Command {
   void execute();
}
 
// in this example, suppose you use a switch to control computer
 
/* The Invoker class */
 class Switch { 
   private List<Command> history = new ArrayList<Command>();
 
   public Switch() {
   }
 
   public void storeAndExecute(Command command) {
      this.history.add(command); // optional, can do the execute only!
      command.execute();        
   }
}
 
/* The Receiver class */
 class Computer {
 
   public void shutDown() {
      System.out.println("computer is shut down");
   }
 
   public void restart() {
      System.out.println("computer is restarted");
   }
}
 
/* The Command for shutting down the computer*/
 class ShutDownCommand implements Command {
   private Computer computer;
 
   public ShutDownCommand(Computer computer) {
      this.computer = computer;
   }
 
   public void execute(){
      computer.shutDown();
   }
}
 
/* The Command for restarting the computer */
 class RestartCommand implements Command {
   private Computer computer;
 
   public RestartCommand(Computer computer) {
      this.computer = computer;
   }
 
   public void execute() {
      computer.restart();
   }
}
 
/* The client */
public class TestCommand {
   public static void main(String[] args){
      Computer computer = new Computer();
      Command shutdown = new ShutDownCommand(computer);
      Command restart = new RestartCommand(computer);
 
      Switch s = new Switch();
 
      String str = "shutdown"; //get value based on real situation
 
      if(str == "shutdown"){
    	  s.storeAndExecute(shutdown);
      }else{
    	  s.storeAndExecute(restart);
      }
   }
}
Category >> Design Patterns Stories  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>

  1. Dani Maietta Esquivel on 2013-12-4

    Hi, this is an excellent contribution. I want to know something, i saw in too much diagrams of this pattern that the client don’t see the invoker, so why in the code, the client can see the invoker?

  2. AMER FAREED on 2014-7-6

    HELLO !
    Because switch is the invoker;and you already studying switch statement in ITCP ………….

  3. Tam on 2014-8-3

    Hi,
    Great article!
    Small correction: the comparison of str should be “contains” instead of “==”.
    “if(str == “shutdown”)” -> Contains

  4. You Know on 2014-12-26

    There was no need for the client to see the Invoker. It should be calling the Interface – that’s what the client should be seeing.

  5. You Know on 2014-12-26

    What-a-whatawhat-what the f* are you talking about?

Leave a comment

*