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 ();}}}