1 Preface
In object orientation, commands are encapsulated in various command objects. Command objects can be passed and reused by different clients at the specified time. The well-designed design pattern is called the Command pattern.
2. Details
2.1 Brief Introduction
The command object encapsulates the information about how to execute commands on the target. Therefore, the client or caller can still perform any operations on the target without having to know any details about the target. By encapsulating a request into an object, the client can parameterize it and place it into the pair class or log, and also support unrecoverable operations. The command object binds one or more actions to a specific receiver. The command mode eliminates the binding between the action that acts as an object and the receiver that executes it.
Encapsulate a request as an object to parameterize the customer with different requests, queue requests or record request logs, and support unrecoverable operations.
2.2 When to use
(1) to allow the application to support revocation and restoration;
(2) You want to parameterize an action of an object to execute the operation, and replace the callback function with different command objects;
(3) specify, sort, and execute requests at different times;
(4) You want to record the modification logs so that these modifications can be redone later in case of system failure;
(5) to enable the system to support transactions, mistakes encapsulate a series of modifications to the data. Transactions can be modeled as command objects.
3 conclusion
The benefits of eliminating the coupling between commands, callers, receivers, and clients in applications are obvious. If a specific command needs to be modified in reality, most of the other components will not be affected. In addition, it is very easy to add a new command class, because you do not have to modify the existing class for this.
The above is all content and I hope it will help you.