First, preface
The command pattern encapsulates a request as an object, allowing you to parameterize the customer with different requests, queue up requests or log requests, and support revocable operations.
Second, the structure diagram
Third, instance code
namespaceCommand mode {classProgram {Static voidMain (string[] args) { //pre-opening preparationBarbecuer boy =NewBarbecuer (); Command BakeMuttonCommand1=NewBakemuttoncommand (boy); Command BakeMuttonCommand2=NewBakemuttoncommand (boy); Command BakeChickenWingCommand1=NewBakechickenwingcommand (boy); Waiter Girl=NewWaiter (); //Open Customer OrderGirl. Setorder (BAKEMUTTONCOMMAND1); Girl. Setorder (BAKEMUTTONCOMMAND2); Girl. Setorder (BAKECHICKENWINGCOMMAND1); //close the order and inform the kitchenGirl. Notify (); Console.read (); } } //Waiter Public classWaiter {Privateilist<command> orders =NewList<command>(); //Set up orders Public voidsetorder (Command command) {if(command.) ToString () = ="Command mode. Bakechickenwingcommand") {Console.WriteLine ("Waiter: Chicken Wing No, please order other barbecue. "); } Else{orders. ADD (command); Console.WriteLine ("Add Order:"+ command. ToString () +"Time:"+DateTime.Now.ToString ()); } } //Cancel Order Public voidcancelorder (Command command) {orders. Remove (command); Console.WriteLine ("Cancel Order:"+ command. ToString () +"Time:"+DateTime.Now.ToString ()); } //all notifications are executed Public voidNotify () {foreach(Command cmdinchorders) {cmd. Excutecommand (); } } } //Abstract Commands Public Abstract classCommand {protectedBarbecuer Receiver; PublicCommand (Barbecuer receiver) { This. Receiver =receiver; } //Execute Command Abstract Public voidExcutecommand (); } //Lamb Kebab Command classBakemuttoncommand:command { PublicBakemuttoncommand (Barbecuer receiver):Base(receiver) {} Public Override voidExcutecommand () {receiver. Bakemutton (); } } //Grilled chicken Wings Command classBakechickenwingcommand:command { PublicBakechickenwingcommand (Barbecuer receiver):Base(receiver) {} Public Override voidExcutecommand () {receiver. Bakechickenwing (); } } //Grill Skewers Public classBarbecuer { Public voidBakemutton () {Console.WriteLine ("Roast kebabs!"); } Public voidbakechickenwing () {Console.WriteLine ("Grilled chicken wings!"); } }}Iv. Summary
Command mode Advantages:
First, it is relatively easy to design a command queue.
Second, the command can be entered into the log, if needed.
Third, allow the party receiving the request to decide whether to veto the request.
Finally, it is easier to implement the revocation and redo of the request.
Since the new specific request command class and other command classes do not affect each other, it is easy to add a specific command class.
The biggest advantage is that the command mode separates a request operand from the object that knows how to perform the operation.
V. Other examples
namespaceCommand mode {classProgram {Static voidMain (string[] args) {Receiver R=NewReceiver (); Command C=NewConcretecommand (R); Invoker I=NewInvoker (); //Set and execute commandI.setcommand (c); I.executecommand (); Console.read (); } } Abstract classCommand {protectedreceiver receiver; PublicCommand (receiver receiver) { This. Receiver =receiver; } Abstract Public voidExecute (); } classConcretecommand:command { PublicConcretecommand (receiver receiver):Base(receiver) {} Public Override voidExecute () {receiver. Action (); } } classReceiver { Public voidAction () {Console.WriteLine ("Execute the request! "); } } classInvoker {PrivateCommand command; Public voidsetcommand (Command command) { This. Command =command; } Public voidExecuteCommand () {command. Execute (); } }}
C # Object-oriented design--command mode (15)