Latest web development tutorials

Command Mode

Command Mode (Command Pattern) is a data-driven design pattern, it belongs to the type of model. Request command in the form of packages in the object, and passed to the calling object. Looking call object can process the appropriate command object and the command is passed to the appropriate object, which executes the command.

Introduction

Intent: Encapsulate a request as an object, so you can use different requests to parameterize clients.

Mainly to solve: in the software system, the behavior of the requester and behavior usually achieve a relationship of tightly coupled, but some situations, such as the need to conduct recording, undo or redo, and other transaction processing, this irresistible tightly coupled design changes is not appropriate.

When to use: In some cases, such as to the behavior of a "record, undo / redo, transactions" and other treatment, such changes can not resist the tight coupling is inappropriate.In this case, how will "conduct requester" and "behavior implementer" decoupling? Acts as a set of abstract objects, loose coupling can be achieved between the two.

How to fix: Run by the recipient to call the caller, in order: the caller receiver → → Command.

The key code: the definition of three roles: 1, received the real command execution object 2, Command 3, invoker objects using the command entry

Application examples: struts 1 core controller ActionServlet in action is only one, the equivalent of Invoker, while class model layer will be as different applications have different model classes, the equivalent of concrete Command.

Advantages: 1, the system reduces the degree of coupling.2, new commands can be easily added to the system.

Disadvantages: command mode may cause some systems have too many specific command class.

Usage scenario: that thelocal command can use the command mode, such as: 1, GUI each button is a command. 2, simulated CMD.

Note: The system needs to support command undo (Undo) and recovery operation (Redo) operation, you can consider using the command mode, see extended command mode.

achieve

We first created as a command interface toOrder,and then create a requestStockclass. Entity and command classesBuyStockSellStock,Orderimplements the interface, it will perform the actual command processing. Created as the call object classBroker,which can accept orders and orders.

Brokerobject using the command mode, based on the type of command to determine which object to perform which commands.CommandPatternDemo,our demonstration classes usingBrokerclass to demonstrate command mode.

Command mode UML diagram

step 1

Create a command interface.

Order.java

public interface Order {
   void execute ();
}

Step 2

Create a request class.

Stock.java

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 entity classOrderimplements the interface.

BuyStock.java

public class BuyStock implements Order {
   private Stock abcStock;

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

   public void execute () {
      abcStock.buy ();
   }
}

SellStock.java

public class SellStock implements Order {
   private Stock abcStock;

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

   public void execute () {
      abcStock.sell ();
   }
}

Step 4

Create command calls the class.

Broker.java

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

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

   public void takeOrder (Order order) {
      orderList.add (order);		
   }

   public void placeOrders () {
      for (Order order: orderList) {
         order.execute ();
      }
      orderList.clear ();
   }
}

Step 5

Use Broker class to accept and execute commands.

CommandPatternDemo.java

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 ();
      broker.takeOrder (buyStockOrder);
      broker.takeOrder (sellStockOrder);

      broker.placeOrders ();
   }
}

Step 6

Verify output.

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