May 27, 2021 Design mode
Command Pattern is a data-driven design pattern that is behavioral. R equests are wrapped in a command and passed on to the calling object. T he calling object looks for a suitable object that can handle the command and passs the command to the appropriate object, which executes the command.
Intent: Encapsulate a request as an object, allowing you to parameterize the customer with different requests.
Key Solution: In software systems, behavior requesters and behavior implementers are usually tightly coupled, but in some cases, such as when behavior needs to be documented, revoked or redone, transactions, etc., this tightly coupled design that cannot resist change is not appropriate.
When to use: In some cases, such as "recording, undoing/redoing, transactions" of behavior, this tight coupling that does not resist change is inappropriate. I n this case, how do you decouple the Behavioral Requester from the Behavioral Implementer? Abstracting a set of behaviors into objects enables a loose coupling between the two.
How to resolve: Execute commands by calling the recipient, in order: the caller → the → command.
Key code: Defines three roles: 1, received real command execution object 2, Command 3, invoker uses the portal of the command object
Application example: ActionServlet, the action core controller in struts 1, has only one, equivalent to Invoker, and classes at the model layer have different model classes with different applications, equivalent to a specific Command.
Pros: 1, reduced system coupling. 2 . New commands can be easily added to the system.
Cons: Using command mode may cause some systems to have too many specific command classes.
Scenario: Command mode can be used where commands are considered, such as: 1, every button in the GUI is a command. 2 , analog CMD.
Note: The system needs to support command revocation (Undo) and Recovery (Redo) operations, or consider using command mode, see the extension of command mode.
We first create the interface Order as a command, and then we create the Stock class as a request. T he entity command classes BuyStock and SellStock implement the Order interface and will perform the actual command processing. Create a class Broker that is the calling object, which accepts orders and can place orders.
Broker objects use command mode to determine which object executes which command based on the type of command. CommandPatternDemo, our demo class uses the Broker class to demonstrate command patterns.
Create a command interface.
Order.java
public interface Order { void execute(); }
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"); } }
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(); } }
Create a command call 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(); } }
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(); } }
Verify the output.
Stock [ Name: ABC, Quantity: 10 ] bought Stock [ Name: ABC, Quantity: 10 ] sold