Big talk Design Pattern _ Command pattern

Source: Internet
Author: User

Take the big bird as an example to go to the barbecue shop and report the food to the waiter. Then the waiter notifies the cook.

Package COM. WZS. design; import Java. text. simpledateformat; import Java. util. arraylist; import Java. util. date; import Java. util. list;/*** big talk design mode -- page238 command mode ** @ author administrator **/public class commandmode {public static void main (string [] ARGs) {// prepare barbecuer boy = new barbecuer (); command bakemuttoncommand1 = new bakemuttoncommand (boy); command bakemuttoncommand2 = new bakemuttoncommand (boy ); Command bakechickenwingcommand = new bakechickenwingcommand (boy); Waiter girl = new waiter (); // opens the door for business girl. setorder (bakemuttoncommand1); girl. setorder (bakemuttoncommand2); girl. setorder (bakechickenwingcommand); // after ordering is complete, inform the kitchen girl. notifya () ;}/ ** kebab */class barbecuer {// kebab public void bakemutton () {system. out. println ("kebab! ") ;}// Roast chicken wings public void bakechickenwing () {system. Out. println (" roast chicken wings! ") ;}}/** Abstract command */abstract class command {protected barbecuer; Public command (barbecuer) {This. barbecuer = barbecuer;} // execute the public abstract void excutecommand ();}/** kebab command */class bakemuttoncommand extends command {public bakemuttoncommand (barbecuer) {super (barbecuer) ;}@ overridepublic void excutecommand () {barbecuer. bakemutton () ;}@ overridepublic string tostring () {RET Urn "command mode, kebab command! ";}}/** Roast chicken wings command */class bakechickenwingcommand extends command {public bakechickenwingcommand (barbecuer) {super (barbecuer) ;}@ overridepublic void excutecommand () {barbecer. bakechickenwing () ;}@ overridepublic string tostring () {return "command mode, roast chicken wings command! ";}}/** Waiter */class waiter {simpledateformat = new simpledateformat (" yyyy-mm-dd hh: mm: SS "); private list <command> orders = new arraylist <command> (); // set the order public void setorder (command) {orders. add (command); system. out. println ("add order:" + command. tostring () + "\ t time:" + simpledateformat. format (new date ();} // cancel the order public void cancelorder (command) {orders. remove (command); system. out. println ("cancel order:" + command. tostring () + "\ t time:" + simpledateformat. format (new date ();} // execute public void notifya () {for (command: Orders) {command. excutecommand ();}}}

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.