1<?PHP2 /*3 * Command mode4 * 5 */6 //declares an interface to perform an operation (command interface)7 InterfaceCook {8 Public functionExcute ();9 }Ten //make rice (concretecommand) One classCookriceImplementsCook { A Private $cooker; - function__construct (Cooker$cooker) { - $this->cooker =$cooker; the } - Public functionExcute () { - $this->cooker->Dorice (); - } + } - //Cooking (Concretecommand) + classCookfoodImplementsCook { A Private $cooker; at function__construct (Cooker$cooker) { - $this->cooker =$cooker; - } - Public functionExcute () { - $this->cooker->Dofood (); - } in } - //Waiter (Invoker) (via list mode, can be further optimized, here slightly) to classServerperson { + Private $cook; - Public functionSetcook (Cook$cook) { the $this->cook =$cook; * } $ Public functionExecuteCommand () {Panax Notoginseng $this->cook->Excute (); - } the } + //Chef (Receiver) A classCooker { the Public functionDorice () { + EchoCooking; - } $ Public functionDofood () { $ Echo' Cooking '; - } - } the //Client - classClient {Wuyi Public Static functionMain () { the $cooker=Newcooker (); - $server=NewServerperson (); Wu //Cooking - $cookA=NewCookrice ($cooker); About $server->setcook ($cookA); $ $server-ExecuteCommand (); - //Make a dish - $cookB=NewCookfood ($cooker); - $server->setcook ($cookB); A $server-ExecuteCommand (); + } the } -Client::main ();
Command mode-design mode-PHP version