From "eat at a fast food restaurant" to command mode (2)

Source: Internet
Author: User

A simple implementation code is provided: The following table lists the standard names of the classes and methods involved in this example that correspond to the command mode:
Waitress (waiter) Invoker
Short order cook) Cycler
Orderup (for chefs) Execute ()
Order (menu, not processed yet) Command
Customer) Client
Takeorder (take the original menu from the customer) Setcommand ()
A typical command mode requires an interface. there is a unified method in the interface, which is "encapsulating commands/requests as objects" (here to better understand the examples in this implementation life, I didn't use the object name method names in the standard command mode. You can find the corresponding standard names according to the above table ):
Public interface order {
Public abstract void orderup ();
}

The specific command/Request Code implements the interface command. to simplify the following, there is only one specific command, that is, the waiter only contacts one customer at this time, note that the orderslip here is the order slip that the waiter processes the original menu obtained from the customer through the menu ), with this in mind, you can easily figure out how to correspond to a specific command role in command mode:
Public class orderslip implements order {
Public cook; // The constructor is passed into a specific Cook to serve as the customer's point. When orderup is called, the cook serves as the receiver of the request. Public orderslip (cook) {This. Cook = cook;} // The orderup method calls the cookmakeburger and cookmakeshakepublic void orderup (){
Cook. cookmakeburger (); // Let a cook. cookmakeshake (); // Let a cook make shake
}
}
}
Cook
 
Cookmakeburger (): void
Cookmakeshake (): void
The waiter uses this menu to complete her task (command object)
Public class waitress {order; Public waitress () {} public void takeorder (Order orderslip) {// This corresponds to setorder () in the standard command mode in the preceding table () order = orderslip ;}
Public void fireorder () {// Note: As mentioned above, the waiter knows that all the // menus support orderup () this method can also be called whenever you need to prepare a meal. Order. orderup ();}
}
What customers do:
Public Class Customer {public static void main (string [] ARGs) {waitress = new waitress (); // The customer calls a waiter cook = new cook (); // because the customer passes through the waiterSimple connectionRequest the cook to cook him/// orderslip = new orderslip (Cook); // The customer calls a menu waitress. takeorder (orderslip); // The waiter obtains the processed menu waitress. fireorder (); // It's time to prepare a meal}
}

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.