Home > Backend Development > C#.Net Tutorial > A brief introduction to the responsibility chain model in C++ design patterns

A brief introduction to the responsibility chain model in C++ design patterns

黄舟
Release: 2017-01-17 13:42:30
Original
1066 people have browsed it

ChainOfResponsibility: Allows multiple objects to have the opportunity to process requests, thereby avoiding the coupling relationship between the sender and receiver of the request. Chain this object and pass the request along the chain until an object handles it.

Benefits of chain of responsibility:

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

So that neither the receiver nor the sender has clear information about the other party, and the objects in the chain themselves do not know the structure of the chain. The result is that chains of responsibility simplify linking objects so that they only need to maintain a single reference to its successor, rather than to all of its candidate recipients.

Client:

[code]//Client
int main(){
    Handler *h1 = new ConcreteHandler;
    Handler *h2 = new ConcreteHandler2;
    Handler *h3 = new ConcreteHandler3;

    //设置职责链上家与下家
    h1->setSuccessor(h2);
    h2->setSuccessor(h3);

    //请求
    int request[8] = {2, 5, 14, 22, 18, 3, 23, 20};
    //循环给最小处理者提交请求,不同的数额由不同权限处理者处理
    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;
}
Copy after login

Class implementation:

[code]//Handler处理类
class Handler{
protected:
    Handler *successor;
public:
    //设置继任者
    void setSuccessor(Handler *successor){
        this->successor = successor;
    }
    //处理请求的抽象方法
    virtual void HandleRequest(int request){}
};

//具体处理类,处理它们负责的请求,可访问的后继者,如果可以处理该请求,就处理之,否则就将该请求转发给它的后继者
class ConcreteHandler: public Handler{
public:
    virtual void HandleRequest(int request)override{
        //0~10在此处理
        if(request >= 0 && request < 10){
            std::cout << "Handler request\n";
        }else if(successor != NULL) //否则转移到下一位
            successor->HandleRequest(request);
    }
};

class ConcreteHandler2: public Handler{
public:
    virtual void HandleRequest(int request)override{
        //10~20在此处理
        if(request >= 10 && request < 20){
            std::cout << "Handler2 request\n";
        }else if(successor != NULL) //否则转移到下一位
            successor->HandleRequest(request);
    }
};

class ConcreteHandler3: public Handler{
public:
    virtual void HandleRequest(int request)override{
        //20~30在此处理
        if(request >= 20 && request < 30){
            std::cout << "Handler3 request\n";
        }else if(successor != NULL) //否则转移到下一位
            successor->HandleRequest(request);
    }
};
Copy after login

The above is the content of a brief understanding of the responsibility chain model of C++ design patterns. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template