Java single application - Architecture Mode - 03. Design mode - 15. Command mode

Original address:
More tutorials: Beam cloud - free course

Command mode

Serial number Chapter in text video
1 Summary -
2 Realization -

Please refer to the navigation above for reading

1. overview

Command Pattern is a data-driven design pattern, which belongs to behavioral pattern. The request is wrapped in the object as a command and passed to the calling object. The calling object looks for the appropriate object that can handle the command, and passes the command to the corresponding object, which executes the command.


Encapsulate a request into an object so that you can parameterize the customer with different requests.

Main solutions:

In software system, behavior requester and behavior implementer are usually tightly coupled, but in some cases, such as when the behavior needs to be recorded, undone or redone, transactions and other processing, this tight coupling design which can not resist changes is not suitable.

When to use:

In some cases, such as "record, undo / redo, transaction" and so on, this tight coupling is not suitable. In this case, how to decouple "behavior requester" from "behavior implementer"? By abstracting a group of behaviors into objects, loose coupling between them can be realized.

How to solve:

Through the caller to call the receiver to execute the command, the order is: caller → receiver → command.

Key code:

Define three roles:

  • received real command execution object
  • Command
  • invoker uses the entry of the command object

Application example:

There is only one action core controller, ActionServlet, in struts 1, which is equivalent to Invoker, and the class of model layer will have different model classes with different applications, which is equivalent to specific Command.


  • The coupling degree of the system is reduced.
  • New commands can be easily added to the system.


Using command mode can cause some systems to have too many specific command classes.

Usage scenario:

The command mode can be used for all places that are considered as commands, such as:

  • Every button in the GUI is a command.
  • Analog CMD.

matters needing attention:

The system needs to support Undo and redo operations of commands. You can also consider using command mode. See the extension of command mode.

2. implementation

We first create the interface Order as a command, and then create the Stock class as a request.

The entity command classes BuyStock and SellStock implement the Order interface, which will execute the actual command processing. Create a class Broker as a calling object that accepts orders and can place them.

Broker objects use the command mode to determine which object executes which command based on the type of command.

CommandPatternDemo, our demonstration class uses the Broker class to demonstrate the command pattern.

Step 1

Create a command interface., the code is as follows:

public interface Order {
   void execute();

Step 2

Create a request class., the code is as follows:

public class Stock {

   private String name = "ABC";
   private int quantity = 10;

   public void buy(){
      System.out.println("Stock [ Name: "+name+", Quantity: " + quantity +" ] bought");
   public void sell(){
      System.out.println("Stock [ Name: "+name+", Quantity: " + quantity +" ] sold");

Step 3

Create an entity class that implements the Order interface., the code is as follows:

public class BuyStock implements Order {
   private Stock abcStock;

   public BuyStock(Stock abcStock){
      this.abcStock = abcStock;

   public void execute() {;
}, the code is as follows:

public class SellStock implements Order {
   private Stock abcStock;

   public SellStock(Stock abcStock){
      this.abcStock = abcStock;

   public void execute() {

Step 4

Create a command call class., the code is as follows:

import java.util.ArrayList;
import java.util.List;

public class Broker {
   private List<Order> orderList = new ArrayList<Order>(); 

   public void takeOrder(Order order){

   public void placeOrders(){
      for (Order order : orderList) {

Step 5

Use the Broker class to accept and execute commands., the code is as follows:

public class CommandPatternDemo {
   public static void main(String[] args) {
      Stock abcStock = new Stock();

      BuyStock buyStockOrder = new BuyStock(abcStock);
      SellStock sellStockOrder = new SellStock(abcStock);

      Broker broker = new Broker();


Step 6

Execute the program and output the result:

Stock [ Name: ABC, Quantity: 10 ] bought
Stock [ Name: ABC, Quantity: 10 ] sold

Last article: Responsibility chain model
Next article: Interpreter mode

If you are interested in the content of the course, you can scan the code to pay attention to our official account or QQ group, and pay attention to our curriculum updates in time.

Tags: Java Struts

Posted on Fri, 13 Mar 2020 07:22:05 -0400 by jwilh