1<?PHP2 /*3 * Responsibility Chain mode4 * 5 * Reference: http://blog.csdn.net/jhq0113/article/details/464544196 * 7 */8 //Apply for Model9 classRequest {Ten //Quantity One Public $num; A //Application Type - Public $requestType; - //Request Content the Public $requestContent; - } - //Abstract Manager - Abstract classManager { + protected $name; - //Senior Manager + protected $manager; A Public function__construct ($_name) { at $this->name =$_name; - } - //set up Manager Supervisor - Public functionSetHeader (Manager$_mana) { - $this->manager =$_mana; - } in //Request for Application - Abstract Public functionApply (Request$_req); to } + //Manager - classCommonmanagerextendsManager { the Public function__construct ($_name) { *Parent::__construct ($_name); $ }Panax Notoginseng Public functionApply (Request$_req) { - if($_req->requesttype = = "Leave" &&$_req->num <= 2) { the Echo"{$this->name}:{$_req->requestcontent} number {$_req->num} is approved. <br/> "; +}Else { A if(isset($this-manager)) { the $this->manager->apply ($_req); + } - } $ } $ } - //Director - classMajordomoextendsManager { the Public function__construct ($_name) { -Parent::__construct ($_name);Wuyi } the Public functionApply (Request$_req) { - if($_req->requesttype = = "Leave" &&$_req->num <= 5) { Wu Echo"{$this->name}:{$_req->requestcontent} number {$_req->num} is approved. <br/> "; -}Else { About if(isset($this-manager)) { $ $this->manager->apply ($_req); - } - } - } A } + //General Manager the classGeneralmanagerextendsManager { - Public function__construct ($_name) { $Parent::__construct ($_name); the } the Public functionApply (Request$_req) { the if($_req->requesttype = = "Leave") { the Echo"{$this->name}:{$_req->requestcontent} number {$_req->num} is approved. <br/> "; -}Else if($_req->requesttype = = "Pay rise" &&$_req->num <= 500) { in Echo"{$this->name}:{$_req->requestcontent} number {$_req->num} is approved. <br/> "; the}Else if($_req->requesttype = = "Pay rise" &&$_req->num > 500) { the Echo"{$this->name}:{$_req->requestcontent} number {$_req->num} say it again. <br/> "; About } the } the } the //--------------------Client---------------------- + $jingli=NewCommonmanager ("manager Li")); - $zongjian=NewMajordomo ("director Guo")); the $zongjingli=NewGeneralmanager ("Sun Total"));Bayi //Set Direct ancestor the $jingli->setheader ($zongjian); the $zongjian->setheader ($zongjingli); - //Application - $req 1=NewRequest (); the $req 1->requesttype = "Leave"; the $req 1->requestcontent = "Vegetable leave!" "; the $req 1->num = 1; the $jingli->apply ($req 1); - $req 2=NewRequest (); the $req 2->requesttype = "Leave"; the $req 2->requestcontent = "Vegetable leave!" "; the $req 2->num = 4;94 $jingli->apply ($req 2); the $req 3=NewRequest (); the $req 3->requesttype = "Raise"; the $req 3->requestcontent = "side dishes ask for a raise!" ";98 $req 3->num = 500; About $jingli->apply ($req 3); - $req 4=NewRequest ();101 $req 4->requesttype = "Raise";102 $req 4->requestcontent = "side dishes ask for a raise!" ";103 $req 4->num = 1000;104 $jingli->apply ($req 4);
Responsibility chain Mode-design mode-PHP version