Big talk design pattern C + + Implementation-24th chapter-Responsibility Chain Model

Source: Internet
Author: User
Tags class manager

First, UML diagram


Second, the concept

Responsibility Chain mode (Chain of Responsibility): enables multiple objects to have the opportunity to process requests, thus avoiding coupling between the sender and receiver of the request. Link the object to a chain and pass the request along the chain, knowing that an object is handling it.


Third, the description

Role:

(1) Handler class : Defines an interface for processing requests.

(2) Concretehandler class: the interface of the specific processing request.

The benefits of the responsibility chain model:

(1) Most crucially, when a customer submits a request, the request is passed along the chain until a Concretehandler object is responsible for processing it.

(2) Neither the receiver nor the sender has clear information about the other, and the object in the chain does not know the structure of the chain. The result is that the chain of responsibilities simplifies the connection of objects, and they only need to keep a reference to the subsequent successor without having to keep all of its candidate recipients ' references.

(3) Because the structure of the chain is defined on the client, the user can add or modify the structure of the processing request at any time . Increased flexibility in assigning responsibilities to objects.

Note: a request is most likely not to be processed at the end of the chain, or because it is not properly configured.


Iv. implementation of C + +

(1) ChainOfResponsibility.h

#ifndef chainofresponsibility_h#define chainofresponsibility_h#include <string> #include <iostream>// Request Class request{private://request type, content, quantity std::string requesttype;std::string requestcontent;int number;public:void SetType (std::string type) {this->requesttype=type;} std::string GetType () {return requesttype;} void SetContent (std::string content) {this->requestcontent=content;} std::string getcontent () {return requestcontent;} void Setnumber (int number) {This->number=number;} int GetNumber () {return this->number;}};/ /handler class, here is the Manager class class manager{protected:std::string name; manager* Superior;public:manager () {}manager (std::string name) {this->name=name;superior=new Manager ();} ~manager () {delete superior;} Set successor void Setsuperior (manager* superior) {This->superior=superior;} Abstract method for processing requests virtual void requestapplication (request* request) {};};/ /concretehandler1: Here is the manager, Commonmanagerclass commonmanager:public Manager{public:commonmanager (std::string name): Manager (name) {}void requesTapplication (request* Request) {if (request->gettype () = = "Leave" &&request->getnumber () <=2) {Std::cout  <<name<< ":" <<request->getcontent () << "Quantity:" <<request->getnumber () << " be approved "&LT;&LT;STD::ENDL;} Can't handle it, move on to the next one for processing. Else{if (superior!=null) {superior->requestapplication (request);}}}};/ /concretehandler2: Here is the director, Majordomoclass majordomo:public Manager{public:majordomo (std::string name): Manager (name) { }void requestapplication (request* Request) {if (request->gettype () = = "Leave" &&request->getnumber () <=5 ) {std::cout<<name<< ":" <<request->getcontent () << "Quantity:" <<request->getnumber () << "approved" &LT;&LT;STD::ENDL;} Can't handle it, move on to the next one for processing. Else{if (superior!=null) {superior->requestapplication (request);}}}};/ /concretehandler3: Here is the general manager, Generalmanagerclass generalmanager:public Manager{public:generalmanager (std::string name ): Manager (name) {}void requestapplication (request* Request) {if (request->gettype () = = "Leave") {std::cout<<name<<": "<<request->getcontent () <<" Quantity: "<<request->getnumber ( ) << "approved" &LT;&LT;STD::ENDL;} else if (request->gettype () = = "Raise" &&request->getnumber () <=500) {std::cout<<name<< ":" <<request->getcontent () << "Quantity:" <<request->getnumber () << "approved" &LT;&LT;STD::ENDL;} else if (request->gettype () = = "Raise" &&request->getnumber () >500) {std::cout<<name<< ":" <<request->getcontent () << "Quantity:" <<request->getnumber () << "Say It Again" <<std::endl;}}}; #endif


(2) Client.cpp

#include "ChainOfResponsibility.h" #include <iostream> #include <cstdlib>//client, client void Main () {Manager * Jinli=new Commonmanager ("Jin Li"); manager* zongjian=new majordomo ("Zong Jian"); manager* zhongjingli=new Generalmanager ("Zhong");//Set up superiors, can be changed according to actual requirements Jinli->setsuperior (Zongjian); zongjian- >setsuperior (Zhongjingli);//The following is a 4 request//Request 1:1 days leave of Absence request request1;request1. SetType ("Leave"); Request1. SetContent ("vegetable Leave"); Request1. Setnumber (1);//The client application is initiated by the "manager", but the actual person who will make the decision is handled by the specific management class, the client does not know Jinli->requestapplication (&AMP;REQUEST1);// Request 2: Leave for 4 days request request2;request2. SetType ("Leave"); Request2. SetContent ("vegetable Leave"); Request2. Setnumber (4);//The client application is initiated by the "manager", but the actual person who will make the decision is handled by the specific management class, the client does not know Jinli->requestapplication (&AMP;REQUEST2);// Request 3: A raise of 500 yuan request request3;request3. SetType ("pay raise"); Request3. SetContent ("Side dishes request a raise"); Request3. Setnumber (500);//The client application is initiated by the "manager", but the actual person who will make the decision is handled by the specific management class, the client does not know Jinli->requestapplication (&AMP;REQUEST3);// Request 4: A raise of 1000 yuan request request4;request4. SetType ("pay raise"); Request4. SetContent ("Side dishes request a raise"); Request4. Setnumber (1000);//client application is initiated by "manager", but the actual person who make the decision is handled by the specific management class, the client does not know Jinli->requestapplication (&AMP;REQUEST4);d elete Jinli,zongjian, Zhongjingli;system ("Pause");}


(3) operation



Big talk design pattern C + + Implementation-24th chapter-Responsibility Chain Model

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.