Intention:
Encapsulates a request as an object so that you can parameterize the customer with different requests, queue requests or log requests, and support undoable operations.
Applicability:
Abstract the action to be executed to parameterize an object, you can use the callback (call back) function in the process language to express this parameterization mechanism. A callback function is a function that registers somewhere before it is called at a later time when it is needed. The Command pattern is an object-oriented alternative to the callback mechanism.
Specify, arrange, and execute requests at different times. A command object can have a lifetime that is independent of the initial request. If the recipient of a request can be expressed in an address space-independent manner, the Command object responsible for the request is routed to a different process and the request is implemented there.
Support Cancel operation. The command's Excute operation stores the state before the operation is implemented, which is used to eliminate the effect of the operation when the operation is canceled. The Command interface must add an Unexecute action that cancels the effect of the last execute call. The commands executed are stored in a history list. You can iterate through this list backwards and forwards and invoke Unexecute and execute separately to achieve the infinite number of "undo" and "Redo".
Support for modifying the log so that when the system crashes, these modifications can be re-done again. Adding mount operations and storage operations to the command interface can be used to maintain a consistent change log for changes. The process of recovering from a crash involves re-reading the recorded commands from disk and re-executing them with the execute operation.
Construct a system with high-level operations built on primitive operations. Such a structure is common in information systems that support transactions (transaction). A transaction encapsulates a set of changes to the data. The command pattern provides a way to model transactions. command has a common interface that allows you to invoke all transactions in the same way. It is also easy to add new transactions to extend the system by using this mode.