C # Object-oriented design--command mode (15)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.