Java Command Mode

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/walkerwqp/article/details/91361214

Mode Command (Command Pattern) is a data-driven design pattern, it belongs to the type of model. Request command in the form of inclusions in a subject, and the object to the caller. Call the object to find the right object can process the command and sends the commands to the appropriate object that executes the command.

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

The main solution: in the software system, behavior and behavior to achieve the requester is usually a relationship of tightly coupled, but some situations, such as the need to conduct record, undo or redo, such as transaction processing, this can not resist tightly coupled design change is not appropriate.

When to use: In some cases, such as to the behavior of a "record, undo / redo, the transaction" and other treatment, such changes can not resist the tight coupling is not appropriate. In this case, how will "act requestor" and "behavior implementer" decoupling? The behavior of a group of abstract objects, loose coupling between the two can be achieved.

How to resolve: call the recipient to execute the command by calling, in this order: the caller recipients → → Command.

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

Application Example: Struts. 1 The core controller ActionServlet only one action, equivalent Invoker, and model-based layer may have different models with different classes of applications, corresponding to the specific Command.

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

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

Usage scenario: think local command can use command mode, such as: 1, GUI each button is a command. 2, analog CMD.

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

Ordeer.java

public interface Ordeer {// Create a command interface
    void Execute ();
}

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");
    }
    
}

BuyStock.java

public class BuyStock implements Ordeer {// Create Order class implements an interface entity
    
    Private Stock abcStock;
    
    public the buyStock (Stock abcStock) {
        this.abcStock = abcStock;
    }
    
    public void Execute () {
        abcStock.buy ();
    }
    
}

SellStock.java

public class SellStock implements Ordeer {
    
    private Stock abcStock;
    
    public SellStock(Stock abcStock) {
        this.abcStock = abcStock;
    }
    
    public void execute() {
        abcStock.sell();
    }
    
}

Borker.java

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


public class Borker { //创建命令调用类
    private List<Ordeer> orderList = new ArrayList<Ordeer>();
    
    public void takeOrder(Ordeer order) {
        orderList.add(order);
    }
    
    public void placeOrdeers() {
        for (Ordeer order : orderList) {
            order.execute();
        }
        orderList.clear();
    }
}

CommandPatternDemo.java

public class CommandPatternDemo { //使用Border类来接受并执行命令
    public static void main(String[] args) {
        Stock abcStock = new Stock();
        
        BuyStock buyStockOrder = new BuyStock(abcStock);
        SellStock sellStockOrder = new SellStock(abcStock);
        
        Borker borker = new Borker();
        borker.takeOrder(buyStockOrder);
        borker.takeOrder(sellStockOrder);
        
        borker.placeOrdeers();
        
    }
}

 

 

Guess you like

Origin blog.csdn.net/walkerwqp/article/details/91361214