PHP design Mode--The responsibility chain Model _php Tutorial

Source: Internet
Author: User
Tags class manager

PHP design mode--responsibility chain mode


The responsibility chain model (also called the Chain of Responsibility model) contains a number of command objects and some processing objects, each processing object determines that it can handle those command objects, it also knows that it should not handle the command object to the next processing object, the pattern also describes the way to add new processing objects to the chain.

UML Class Diagrams:

Role:

Abstract Processor (Manager): Defines an interface for processing requests. If necessary, the interface can define a method to set and return a reference to the home. This role is usually implemented by an abstract class or interface.

Specific processor (Commonmanager): When the specific processor receives the request, it can choose to dispose of the request or pass the request to the other person. As the specific processor holds references to the homes, the specific processor can access the homes if necessary.


Core code:

 name = $_name;    }//Set Manager Upper Public function SetHeader (manager $_mana) {$this->manager = $_mana; }//apply for the abstract public function apply (request $_req);}    Manager Class Commonmanager extends manager{public function __construct ($_name) {parent::__construct ($_name);        Public Function Apply (Request $_req) {if ($_req->requesttype== leave && $_req->num<=2)        {echo {$this->name}:{$_req->requestcontent} number {$_req->num} is approved.; } else {if (Isset ($this->manager)) {$this->manager->apply ($_r            EQ); }}}}//Director class Majordomo extends manager{public function __construct ($_name) {Parent::__construc    T ($_name);        } Public Function Apply (Request $_req) {if ($_req->requesttype = = Leave && $_req->num <= 5) {echo {$this->name}:{$_req->requestcontent} number {$_req->num} is approved.; } else {if (Isset ($this->manager)) {$this->manager->apply ($_            REQ); }}}}//general Manager class Generalmanager extends manager{public function __construct ($_name) {Parent::__co    NStruct ($_name); Public Function Apply (Request $_req) {if ($_req->requesttype = = leave) {echo {$this-&gt        ; name}:{$_req->requestcontent} number {$_req->num} is approved.; } else if ($_req->requesttype== raise && $_req->num <=) {echo {$this->name}:{        $_req->requestcontent} number {$_req->num} is approved.; } else if ($_req->requesttype== pay && $_req->num>500) {echo {$this->name}:{$_r        Eq->requestcontent} number {$_req->num} say it again.; }    }}

Calling client code:

Header (content-type:text/html;charset=utf-8);//--------------------Responsibility Chain mode----------------------require_once. responsibility/responsibility.php, $jingli = new Commonmanager (manager li), $zongjian = new Majordomo (director Guo); $zongjingli = new Generalmanager (Sun total);//Set Direct superior $jingli->setheader ($zongjian); $zongjian->setheader ($zongjingli);//Apply $req1 = new Request (); $req 1->requesttype = Leave of absence, $req 1->requestcontent = vegetable leave!; $req 1->num = 1; $jingli->apply ($req 1); $ REQ2 = new Request (), $req 2->requesttype = leave of absence, $req 2->requestcontent = vegetable leave!; $req 2->num = 4; $jingli->apply ($ REQ2); $req 3 = new request (); $req 3->requesttype = raise; $req 3->requestcontent = side dish request a raise!; $req 3->num = $; $jingli-& Gt Apply ($req 3); $req 4 = new request (); $req 4->requesttype = salary increase; $req 4->requestcontent = side dish request a raise!; $req 4->num = 1000;$ Jingli->apply ($req 4);

Applicable scenarios:

1. There are multiple objects that can handle the same request, and which object handles the request automatically determined by the run time.

2. Submit a request to one of several objects without explicitly specifying the recipient.

3. You can dynamically specify a set of object processing requests.


At this point, the PHP design mode series of tutorials are all updated to the end, you are welcome to criticize. Your words are the driving force of my progress.


http://www.bkjia.com/PHPjc/1015539.html www.bkjia.com true http://www.bkjia.com/PHPjc/1015539.html techarticle PHP Design mode--responsibility chain mode responsibility chain mode (also called Chain of responsibility mode) contains some command objects and some processing objects, each processing object determines it can handle those command objects ...

  • Related Article

    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.