# Command

In 
Published 2022-12-03

This tutorial explains to you the design pattern named Command (which is a Structural Pattern).

# Command Pattern - theory

Command Design Pattern in Java is used when we don't want to call a class method directly through the client code. Sometimes you might want to create a list of commands and run them later.

Take a look at the following UML diagram representing the Command design pattern (for my example):

In this example you can see there is an interface which acts as a "command interface". Using this interface we create "concrete commands" (SellCommands, BuyCommands, ViewCommands classes). These classes encapsulate a request (one command). These commands are executed by an "invoker" (the Invoker class in my example). The invoker object is created and used in the client code (CommandJavaPatternExample class in my example). The "Stock" class in the Receiver of the command.

# Command Pattern - example

Here is an example of using the Command Design Pattern in Java:

package command.java.pattern.example;
 
// The Stock class is the Receiver of the command .
public class Stock {
      
     private String name;
     private int quantity;
 
     public Stock(String name) {
         super();
         this.name = name;
         this.quantity = 0;
        }
      
     public void buy(int quantity){
         this.quantity = this.quantity + quantity;
         System.out.println("I have bought "+quantity+ " "+name);
     }
      
     public void sell(int quantity){
         this.quantity = this.quantity - quantity;
         System.out.println("I have sold "+quantity+ " "+name);
     }
      
     public void view(){
         System.out.println("The quantity of "+name+" = " + quantity);
     }
}
package command.java.pattern.example;
 
public interface CommandInterface {
    public void runCommand();
}
package command.java.pattern.example;
 
public class SellCommand implements CommandInterface {
 
    private Stock stock1;
    private int quantity;
     
    public SellCommand(Stock stock1, int quantity){
          this.stock1 = stock1;
          this.quantity = quantity;
    }
 
    @Override
    public void runCommand() {
        stock1.sell(this.quantity);
    }
}
package command.java.pattern.example;
 
public class BuyCommand implements CommandInterface {
 
    private Stock stock1;
    private int quantity;
     
    public BuyCommand(Stock stock1, int quantity){
          this.stock1 = stock1;
          this.quantity = quantity;
    }
 
    @Override
    public void runCommand() {
        stock1.buy(this.quantity);
    }
}
package command.java.pattern.example;
 
public class ViewCommand implements CommandInterface {
 
    private Stock stock1;
     
    public ViewCommand(Stock stock1){
          this.stock1 = stock1;
    }
 
    @Override
    public void runCommand() {
        stock1.view();
    }
}
package command.java.pattern.example;
 
// The Invoker class call the commands using the concrete Command classes
public class Invoker {
 
    private CommandInterface command; 
     
    public Invoker(CommandInterface command) {
        super();
        this.command = command;
    }
 
    public void execute(){
        command.runCommand();
    }
     
}
package command.java.pattern.example;
 
public class CommandJavaPatternExample {
 
    public static void main(String[] args) {
         
         
         
        Stock pears = new Stock("pears");
        Stock apples = new Stock("apples");
         
        BuyCommand pearsBuyCommand = new BuyCommand(pears, 10);
        SellCommand pearsSellCommand = new SellCommand(pears, 3);
        ViewCommand pearsViewCommand = new ViewCommand(pears);
         
        BuyCommand applesBuyCommand = new BuyCommand(apples, 3);
        ViewCommand applesViewCommand = new ViewCommand(apples);
         
        BuyCommand pearsBuyCommand2 = new BuyCommand(pears, 10);
        ViewCommand pearsViewCommand2 = new ViewCommand(pears);
         
 
        System.out.println("---------------------------------");
        Invoker invoker1 = new Invoker(pearsBuyCommand); invoker1.execute();
        Invoker invoker2 = new Invoker(pearsSellCommand); invoker2.execute();
        Invoker invoker3 = new Invoker(pearsViewCommand); invoker3.execute();
         
        System.out.println("---------------------------------");
        Invoker invoker4 = new Invoker(applesBuyCommand); invoker4.execute();
        Invoker invoker5 = new Invoker(applesViewCommand); invoker5.execute();
         
        System.out.println("---------------------------------");
        Invoker invoker6 = new Invoker(pearsBuyCommand2); invoker6.execute();
        Invoker invoker7 = new Invoker(pearsViewCommand2); invoker7.execute();
        System.out.println("---------------------------------");
    }
 
}

When you run this example you will receive the following result: