13. Command mode of design mode

Command mode of design mode

table of Contents

Command mode of design mode

1. Basic introduction

Two, UML diagram

Three, the precautions and details of the command mode


1. Basic introduction

1) Command Pattern: In software design, we often need to send requests to certain objects, but we don’t know who the recipient of the request is, and we don’t know which operation is being requested. We just need to Just specify the specific request recipient at runtime, at this time, you can use the command mode to design

2) The naming pattern allows the request sender and request receiver to eliminate the coupling between each other, make the calling relationship between objects more flexible, and achieve decoupling.

3) In the naming mode, a request is encapsulated as an object, so that different parameters are used to express different requests (namely), and the command mode also supports undoable operations.

4) Easy-to-understand understanding: The general issues orders and the soldiers execute them. There are several roles: general (command issuer), soldier (specific executor of command), command (connect general and soldier). Invoker is the caller (general), Receiver is the callee (soldier), MyCommand is the command, which implements the Command interface and holds the receiving object

Two, UML diagram

Three, the precautions and details of the command mode

1) Decouple the object that initiated the request from the object that executed the request. The object that initiates the request is the caller. The caller can make the receiver work as long as the execute() method of the command object is called, without knowing who the specific receiver object is and how it is implemented. The command object will be responsible for letting the receiver execute The requested action, that is, the decoupling between the "request initiator" and the "request executor" is achieved through the command object, which acts as a bridge.

2) It is easy to design a command queue. As long as the command object is placed in the queue, the command can be executed in multiple threads

3) It is easy to revoke and redo the request

4) Insufficient command mode: some systems may have too many specific command types, which increases the complexity of the system. This point should be paid attention to when using it.

5) Null command is also a design pattern, it saves us the operation of null. In the above example, if there is no empty command, we will be blank every time we press a button, which brings us some trouble in coding.

6) The classic application scenario of the command mode: a button on the interface is a command, simulating CMD (DOS command), order cancellation/recovery, trigger-feedback mechanism

 

Guess you like

Origin blog.csdn.net/qq_45072383/article/details/114105237