C + + Design mode--A brief analysis of responsibility chain mode

Source: Internet
Author: User
Responsibility chain Mode (chainofresponsibility): Enables multiple objects to have the opportunity to process requests, thus avoiding the coupling between the sender and receiver of the request. Link the object to a chain and pass the request along the chain until an object handles it.

The benefits of the responsibility chain:

When a customer submits a request, the request is passed along the chain until a Concretehandler object is responsible for processing it.

So that both the receiver and the sender have no explicit information about each other and the object in the chain does not know the structure of the chain. The result is a chain of responsibilities that simplifies the linking of objects by simply maintaining a reference to the successor, without having to keep all of its candidate references.

Client:

[Code]//clientint Main () {    Handler *h1 = new Concretehandler;    Handler *h2 = new ConcreteHandler2;    Handler *h3 = new ConcreteHandler3;    Set up responsibility chain Home and home    h1->setsuccessor (H2);    H2->setsuccessor (H3);    Request    int Request[8] = {2, 5, +, +, 3, +};    The loop submits the request to the minimum processor, and the different amounts are handled by different permission handlers for    (auto i = 0; i < 8; ++i) {        h1->handlerequest (request[i]);    }    Output:    //     Handler request    //     Handler request/    /     Handler2 request    //     Handler3 request/    /     Handler2 request    //     Handler request    //     Handler3 request    //     Handler3 request    return 0;}

Class implementation:

[Code]//handler Processing Class Handler{protected:handler *successor;public://Set successor void Setsuccessor (Handler *successo    r) {this->successor = successor; }//abstract method for processing requests virtual void HandleRequest (int request) {}};//specifically handles the class, handles the request that they are responsible for, the accessible successor, if the request can be processed, or forwards the request to its successor CL Concretehandler:public handler{public:virtual void handlerequest (int request) override{//0~10 in this process I        F (Request >= 0 && Request <) {Std::cout << "Handler request\n";    }else if (successor! = NULL)//otherwise transferred to the next successor->handlerequest (request);        }};class concretehandler2:public handler{public:virtual void handlerequest (int request) override{//10~20 in this process        if (Request >= && request <) {Std::cout << "Handler2 request\n";    }else if (successor! = NULL)//otherwise transferred to the next successor->handlerequest (request); }};class concretehandler3:public handler{public:virtual VoidHandleRequest (int request) override{//20~30 here to process if (Request >= && Request < 30) {        Std::cout << "Handler3 request\n";    }else if (successor! = NULL)//otherwise transferred to the next successor->handlerequest (request); }};

The above is the C + + design mode of knowledge of the responsibility chain mode of content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.