order

Command mode , Encapsulates a command as an object , It enables the client program to use commands through a unified command interface , You don't have to know the implementation details of the command . And the order of commands can be sorted and recorded , To implement the Undo function .

Motivation and structure

At some point , The object that receives the request , Don't know how to execute the request . perhaps , Designers don't want them to know how to execute requests . This encapsulates the command as an object , This object is the same as other objects , Can be stored and delivered . In this way, the object receiving the command is decoupled from the real command operation .
further more , Abstract all commands into the same interface , In this way, it is more flexible to update and replace commands . Specifically speaking , Abstract one Command
class , It defines the interface to execute commands .Command A subclass of implements this interface , Specify the command recipient to take the corresponding action .
Its structure is as follows :

Sometimes , A command needs to execute a series of commands . For this general requirement , Define a MacroCommand Subclasses of , It stores multiple in order Command
Subclasses of , And call their interfaces in order .

Such a design , Make the design command related content ( For example, the user interface ) Time , It's very flexible . The root of all this , This is because the object submitting a command request only needs to know how to submit the request , You don't need to know how to execute a real command .

Timing of use

* As an object-oriented substitute for callback mechanism
* Designated at different times , Arrange and execute requests . If the receiver of a request can be expressed in an address space independent way , Then we can take the negative
The command object responsible for the request is passed to a different process and the request is implemented there .
* Used to support undo operations .Command Object can hold a list of states , And add a undo operation interface .
* Used to support replay logs .
* Used to support transactions . Because of the consistent interface , So it's easy to build a transaction with a series of commands .
Working methods

assembly

* Command: Declare the interface to perform the operation
* ConcreteCommand: Bind a receiver object to an operation , Call the corresponding operation of the receiver , To implement the interface
* Client: Create a specific command object and set its receiver
* Invoker: Ask the command to execute the request
* Receiver: Know how to implement operations related to executing a request . Any class can act as a receiver
cooperation

* Client Create a ConcreteCommand Object and specify its Receiver object
* some Invoker Object to store the ConcreteCommand object .
* The Invoker By calling Command Object's Execute Operation to submit a request . If the command is revocable ,
ConcreteCommand It's in execution Excute Stores the current state before the operation to cancel the command .
* ConcreteCommand Object to call its Receiver To perform the request .
Advantages and disadvantages

advantage

* Command Patterns decouple the object calling the operation from the object that knows how to implement it .
* Command Is a class of objects . They can be manipulated and extended like any other object .
* You can assemble multiple commands into a composite command . For example, it was described earlier MacroCommand class . Generally speaking , The composite command is Composite An example of a pattern .
* Add new Command be prone to , Because there is no need to change existing classes .
Key points of realization

* How to define the intelligence level of a command object . One extreme : Know only the receiver and the action performed . The other extreme : All commands are executed in the command object . compromise : The receiver can be obtained dynamically .
* Methods to support Undo and redo commands . You need to store information :

* receiver object , It actually performs the operations that process the request .
* Parameters to perform the operation on the receiver .
* If processing the request changes some values in the recipient object , Then these values must also be stored first . The receiver must also provide some operations , To enable the command to return the recipient to its previous state .
* If multiple revocation is supported , You also need a list to store this information .
* Avoid error accumulation during cancellation operations , have access to Memento pattern

Technology
©2019-2020 Toolsou All rights reserved,
QQ Login interface implementation code implementation mysql Addition, deletion, modification and query of database JAVA Experiment 4 set and functional programming experiment about String How to create objects VHDL——4 choose 1 Data selector C language ( Guess numbers games ) Blue Bridge Cup MCU advanced module --NE555 I don't memorize eight part essays , You go to the interview ? Hill sorting of sorting algorithm ——c++ realization python What does built-in function mean _python What are the built-in functions