6.17. Command mode

发布时间 :2025-10-25 12:24:44 UTC      

Command pattern (Command Pattern) is a data-driven design pattern, which belongs to behavioral pattern. The request is wrapped in the object in the form of a command and passed to the calling object. The calling object finds the appropriate object that can process the command, and passes the command to the corresponding object, which executes the command.

6.17.1. Introduction

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

主要解决: In software systems, there is usually a tightly coupled relationship between behavior requesters and behavior implementers, but in some situations, such as the need to record, undo or redo behavior, transactions and other processing, this tightly coupled design that can not resist changes is not appropriate.

何时使用: In some situations, such as “recording, undoing / redoing, transactions” and so on, the tight coupling that cannot resist change is not appropriate. In this case, how to decouple the “behavior requester” from the “behavior implementer”? By abstracting a group of behaviors into objects, the loose coupling between the two can be realized.

如何解决: The caller invokes the recipient to execute the command, in the order that the caller → commands → the recipient.

关键代码: Define three roles: 1, received real command execution object 2, Command 3, invoker uses the entry of the command object

应用实例: There is only one action core controller ActionServlet in struts 1, which is equivalent to Invoker, and the class of the model layer will have different model classes according to different applications, which is equivalent to the concrete Command.

优点: 1. The coupling degree of the system is reduced. 2. New commands can be easily added to the system.

缺点: Using command mode may result in some systems having too many specific command classes.

使用场景: Command mode can be used wherever you think it is a command, for example: 1. Every button in GUI is a command. 2. Simulate CMD.

注意事项: The system needs to support command undo (Undo) operation and restore (Redo) operation, you can also consider using command mode, see the extension of command mode.

Schematic diagram of command mode structure:

image0

6.17.2. Realize

Let’s first create an interface as a command Order And then create the Stock Class. Entity command class BuyStock And SellStock Has been realized Order Interface, the actual command processing will be performed. Create a class as a calling object Broker Which accepts orders and can place orders

Broker Object uses command mode to determine which object executes which command based on the type of command. CommandPatternDemo Class usage Broker Class to demonstrate command mode.

image1

6.17.3. Step 1

Create a command interface.

Order.java

publicinterfaceOrder{voidexecute();}

6.17.4. Step 2

Create a request class.

Stock.java

publicclassStock{privateStringname="ABC";privateintquantity=10;publicvoidbuy(){System.out.println("Stock
[ Name:"+name+", Quantity:"+quantity+"]
bought");}publicvoidsell(){System.out.println("Stock [ Name:"+name+",
Quantity:"+quantity+"] sold");}}

6.17.5. Step 3

Create and implement the Order The entity class of the interface.

BuyStock.java

publicclassBuyStockimplementsOrder{privateStockabcStock;publicBuyStock(StockabcStock){this.abcStock=abcStock;}publicvoidexecute(){abcStock.buy();}}

SellStock.java

publicclassSellStockimplementsOrder{privateStockabcStock;publicSellStock(StockabcStock){this.abcStock=abcStock;}publicvoidexecute(){abcStock.sell();}}

6.17.6. Step 4

Create a command call class.

Broker.java

importjava.util.ArrayList;importjava.util.List;publicclassBroker{privateList<Order>orderList=newArrayList<Order>();publicvoidtakeOrder(Orderorder){orderList.add(order);}publicvoidplaceOrders(){for(Orderorder:orderList){order.execute();}orderList.clear();}}

6.17.7. Step 5

Use the Broker class to accept and execute commands.

CommandPatternDemo.java

publicclassCommandPatternDemo{publicstaticvoidmain(String[]args){StockabcStock=newStock();BuyStockbuyStockOrder=newBuyStock(abcStock);SellStocksellStockOrder=newSellStock(abcStock);Brokerbroker=newBroker();broker.takeOrder(buyStockOrder);broker.takeOrder(sellStockOrder);broker.placeOrders();}}

6.17.8. Step 6

Execute the program and output the result:

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

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.