The client only wants to send commands or requests. It does not care who the real receiver of the request is or how it is implemented. Moreover, the action of the same request can have different request content, of course, the specific processing functions are different. How can this problem be achieved? Next we will learn the command mode to encapsulate a request as an object, so that you can use different requests to parameterize the customer, queue the request or record the request log, and supports unrecoverable operations. Detailed Structure Diagram parsing Command: defines the Command interface and declares the execution method. ConcreteCommand: The Implementation object of the command interface, which is a "virtual" implementation. It usually holds the receiver and calls the receiver's function to complete the operation to be executed by the command. Receiver: the recipient of the object that actually executes the command. Any class may become a receiver, as long as it can implement the corresponding functions required by the command. Invoker: requires the command object to execute the request. It usually holds the command object and can hold many command objects. This is the place where the client actually triggers the command and requires the command to execute the corresponding operation, that is, it is equivalent to the entry of the command object. Client: create a specific command object and set the receiver of the command object. Note that this is not a Client in the general sense, but an assembly of command objects and recipients. It may be better to call this Client an assembler, because the client that actually uses the command triggers the execution from Invoker. Corresponding code [csharp] <span style = "font-size: 18px;"> class Program {static void Main (string [] args) {Receiver r = new Receiver (); command c = new ConcreteCommand (r); Invoker I = new Invoker (); // Set and execute command I. setCommand (c); I. executeCommand (); Console. read () ;}/// declare the operation execution interface abstract class Command {protected extends er; public Command (extends er extends ER) {this. cycler = Cycler ;} Abstract public void Execute () ;}// ConcreteCommand class, binds a receiver object to an operation, and calls the corresponding operation of the receiver to Execute class ConcreteCommand: command {public ConcreteCommand (extends er extends ER): base (extends ER) {} public override void Execute () {extends er. action () ;}} // knows how to implement and execute a request-related operation. Any class may act as a Receiver class aggreger {public void Action () {Console. writeLine ("execution request! ") ;}} // To execute this request class Invoker {private Command command Command; public void SetCommand (command Command) {this. command = command;} public void ExecuteCommand () {command. execute () ;}</span> example Command mode in life encapsulates a request as an object, so that different requests can parameterize the customer, the bill for dining is an example of the Command mode. The waiter accepts the order from the customer and writes it into the bill. The order is queued for preparation, and the bill does not depend on the menu, it can be used by different customers, or added to different single-point projects. Command mode instance profiling Command mode it encapsulates commands and separates the responsibility of the Command sender from that of the Command executor. We know that a class is a combination of operations and corresponding variables, in the course of ordering meals, there is a class of Waiter [csharp] <span style = "font-size: 18px; "> </span> [csharp] <span style =" font-size: 18px; ">/// Waiter public class Waiter {private IList <Command> orders = new List <Command> (); // set the order public void SetOrder (Command command) {if (command. toString () = "command mode. bakeChickenWingCommand ") {Console. writeLine ("Waiter: no chicken wings, please click something else Barbecue. ");} Else {orders. add (command); Console. writeLine ("add order:" + command. toString () + "time:" + DateTime. now. toString () ;}}// cancel the order public void CancelOrder (Command command) {orders. remove (command); Console. writeLine ("cancel order:" + command. toString () + "time:" + DateTime. now. toString ();} // execute public void Policy () {foreach (Command cmd in orders) {cmd. excuteCommand () ;}}</span> below we need to abstract an abstract command, Seto Rder, CancelOrder, and other methods are all provided by Command. If a Command object is generated separately, function-level functions are mentioned at the class level, abstract commands are very important ., Other methods and command classes inherit from this abstract class. [Csharp] <span style = "font-size: 18px;"> // abstract Command public abstract class Command {protected Barbecuer receiver; public Command (Barbecuer receiver) {this. extends ER = extends er;} // execute the Command abstract public void ExcuteCommand ();} // kebab Command class BakeMuttonCommand: Command {public BakeMuttonCommand (Barbecuer receiver): base (extends ER) {} public override void ExcuteCommand () {extends er. bakeMutton () ;}// bake Chicken wings Command class BakeChickenWingCommand: Command {public BakeChickenWingCommand (Barbecuer receiver): base (worker ER) {} public override void ExcuteCommand () {worker er. bakeChickenWing () ;}</span> [csharp] <span style = "font-size: 18px;"> // public class Barbecuer {public void BakeMutton () {Console. writeLine ("kebab! ");} Public void BakeChickenWing () {Console. WriteLine (" roast chicken wings! ") ;}</Span> call the client [csharp] <span style =" font-size: 18px; "> static void Main (string [] args) {// preparations before opening a shop Barbecuer boy = new Barbecuer (); Command bakeMuttonCommand1 = new BakeMuttonCommand (boy); Command bakeMuttonCommand2 = new BakeMuttonCommand (boy ); command bakeChickenWingCommand1 = new BakeChickenWingCommand (boy); Waiter girl = new Waiter (); // a la carte for customers who open the door. setOrder (bakeMuttonCommand1); girl. S EtOrder (bakeMuttonCommand2); girl. setOrder (bakeChickenWingCommand1); // when the order is closed, inform the kitchen girl. Y (); Console. read () ;}</span> in the client program, commands such as SetOrder, CancelOrder, modify order, and so on of Waiter are encapsulated by Command, so that one of its key abstract classes is the Command class, which defines an operation interface. At the same time, we can see that these three commands are just methods, however, the Command mode mentioned them to the class level. Although it violates the object-oriented principle, it solves the issue of separating the requestor and executor of commands. Command time 1: You must specify the request at different times and queue the request. 2: The system must support command revocation. 3: If a system wants to update all the data in the system to the log, so that when the system crashes, it can read all the data update commands in the log and re-call Execute () to restore the data updates made by the system before the crash. The role of command mode 1: It is easy to design a command queue 2: when needed, you can easily teach the command to be included in log 3: allow the receiving party to decide whether to reject the request 4: It is easy to cancel and redo the request 5: Because the tightening of the specific command class does not affect other classes, therefore, it is easy to add a new command class. The command mode separates the objects of an operation requested from the objects that know how to execute an operation. Summary The Command mode easily decouples "behavior requestor" from "behavior implementer ".