command mode
Command Pattern is a data-driven design pattern, which is a behavioral pattern. The request is wrapped in an object in the form of a command and passed to the calling object. The calling object looks for a suitable object that can handle the command, and passes the command to the corresponding object, which executes the command.
Introduction
Intent:Encapsulate a request into an object, allowing you to parameterize the client with different requests change.
Main solution: In software systems, behavior requesters and behavior implementers usually have a tightly coupled relationship, but in some cases, such as the need to record, cancel, or This kind of tightly coupled design that cannot resist changes is not suitable when redoing, transactions, etc. are processed.
When to use: In some situations, such as "recording, undo/redo, transaction" and other processing of behaviors, this kind of tight coupling that cannot resist changes is inappropriate. of. In this case, how to decouple "behavior requester" from "behavior implementer"? Abstracting a set of behaviors into objects can achieve loose coupling between them.
How to solve: Call the acceptor through the caller to execute the command, in the order: caller→recipient→command.
Key code: Define three roles: 1. received is the real command execution object 2. Command 3. invoker uses the entrance of the command object
Application example : Struts 1 There is only one action core controller ActionServlet, which is equivalent to Invoker, and the model layer class will have different model classes with different applications, which is equivalent to a specific Command.
Advantages: 1. Reduces system coupling. 2. New commands can be easily added to the system.
Disadvantages: Using command mode may cause some systems to have too many specific command classes.
Usage scenarios: Command mode can be used wherever commands are considered, such as: 1. Every button in the GUI is a command. 2. Simulate CMD.
Note: The system needs to support the undo (Undo) operation and recovery (Redo) operation of the command. You can also consider using the command mode. See the extension of the command mode.
Implementation
We first create the interface Order as the command, and then create the Stock class as the request. The entity command classes BuyStock and SellStock implement the Order interface and will perform actual command processing. Create a class Broker as the calling object, which accepts orders and can place orders.
Broker Objects use the command pattern to determine which object executes which command based on the type of command. CommandPatternDemo, our demo class uses the Broker class to demonstrate the command pattern.
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 an entity class that implements the Order 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 a command calling 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 the 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 the output.
Stock [ Name: ABC, Quantity: 10 ] bought Stock [ Name: ABC, Quantity: 10 ] sold