Intent:
Encapsulate a request as an object so that you can parameterize the customer with different requests, queue requests or record request logs, and support unrecoverable operations.
Applicable environment:
(1) abstract the action to be executed to parameterize an object. You can use the callback function in the process language to express this parameterization mechanism. A callback function is a function that is registered at a certain place and will be called at a later time. The command mode is an object-oriented alternative to the callback mechanism.
(2) specify, sort, and execute requests at different times. A command object can have a lifetime unrelated to the initial request. If the recipient of a request can be expressed in a way unrelated to the address space, the command object responsible for the request can be sent to another different process and implemented there.
(3) cancel the operation. The Command's excute operation can store the status before the operation is implemented. when the operation is canceled, this status is used to eliminate the impact of this operation. You must add a unexcute operation to the command interface. This operation cancels the effect of the last excute call. The executed command is stored in a history list. You can traverse the list backward and forward, and call unexcute and excute respectively to "cancel" and "redo" the number of duplicates is unlimited ".
(4) logs can be modified so that the modifications can be redone when the system crashes. Add the loading and storage operations to the command interface to maintain a consistent change log. Recovery from a crash involves re-reading recorded commands from the disk and re-executing them with the excute operation.
(5) construct a system using high-level operations built on primitive operations. Such a structure is common in information systems that support transactions. A transaction encapsulates a group of changes to the data. Mode provides a method for modeling transactions. Command has a public interface that allows you to call all transactions in the same way. At the same time, it is easy to add new transactions to expand the system.
Structure: