[Design mode] salary increase requires approval by the boss-responsibility chain mode

Source: Internet
Author: User
Tags class manager

I. Overview

Definition: Enables multiple objects to process requests to avoid coupling between request senders and receivers. Connect the object to a chain and pass the request along the chain until an object processes it.

Advantage: when a customer initiates a request, the request is passed along the chain until a concretehandler object is responsible for processing it.

 

 

2. Basic responsibility chain code

# Include <iostream> using namespace STD; Class handler {protected: static handler * successor; public: void setsuccessor (handler * successor) {This-> successor = successor ;} virtual void handlerequest (INT request) {}}; handler * Handler: successor = NULL; // class concretehandler1: Public handler {public: void handlerequest (INT request) {If (request> = 0 & request <10) {cout <"Concretehandler1" <"processing requests" <request <Endl;} else if (successor! = NULL) {successor-> handlerequest (request) ;}}; class concretehandler2: Public handler {public: void handlerequest (INT request) {If (request >=10 & request <20) {cout <"concretehandler2" <"request processing" <request <Endl;} else if (successor! = NULL) {successor-> handlerequest (request) ;}}; class concretehandler3: Public handler {public: void handlerequest (INT request) {If (request >=20 & request <30) {cout <"concretehandler3" <"request processing" <request <Endl;} else if (successor! = NULL) {successor-> handlerequest (request) ;}}; int main () {handler * H1 = new concretehandler1 (); handler * H2 = new concretehandler2 (); handler * h3 = new concretehandler3 (); H1-> setsuccessor (H2); // set the next H2-> setsuccessor (H3 ); // set the next int request [] = {2, 5, 14, 22, 18, 3, 27, 20}; For (INT I = 0; I <8; ++ I) {H1-> handlerequest (request [I]);} return 0 ;}

Iii. Example

The food supplier requires a salary increase, and the general manager is required to approve the salary increase. The manager is handed over to the Director, and the director is handed over to the general manager.

Disadvantage: The manager class is too long and has too many branches. If you want to add a Project Manager or department manager, you need to change the original category.

# Include <iostream> using namespace STD; // Enum managerlevel {"manager", "director", "General Manager"} managerlevel; Enum managerlevel {manager, chief_inspector, President }; // apply for class request {// apply for category PRIVATE: String requesttype; public: String getrequesttype () {return requesttype;} void setrequesttype (string value) {requesttype = value ;} // application content PRIVATE: String requestcontent; public: String getrequestcontent () {return requestcontent;} void setrequestcontent (string value) {requestcontent = value;} // quantity PRIVATE: int number; public: int getnumber () {return number;} void setnumber (INT value) {This-> Number = value ;}; // manager class manager {protected: string name; public: Manager (string name) {This-> name = Name;} // obtain the result void getresult (managerlevel, request) {If (managerlevel = Manager) {If (request. getrequesttype () = "leave" & request. getnumber () <= 2) {// console. writeline ("{0 }:{ 1} quantity {2} approved", name, request. requestcontent, request. number); cout <name <request. getrequestcontent () <"quantity" <request. getnumber () <"approved" <Endl;} else {cout <name <request. getrequestcontent () <"quantity" <request. getnumber () <"I do not have permission to process" <Endl; // console. writeline ("{0 }:{ 1} count {2} I do not have permission to process", name, request. requestcontent, request. number) ;}} else if (managerlevel = chief_inspector) {If (request. getrequesttype () = "leave" & request. getnumber () <= 5) {cout <name <request. getrequestcontent () <"quantity" <request. getnumber () <"approved" <Endl; // console. writeline ("{0 }:{ 1} quantity {2} approved", name, request. requestcontent, request. number);} else {cout <name <request. getrequestcontent () <"quantity" <request. getnumber () <"I do not have permission to process" <Endl; // console. writeline ("{0 }:{ 1} count {2} I do not have permission to process", name, request. requestcontent, request. number) ;}} else if (managerlevel = President) {If (request. getrequesttype () = "leave") {cout <name <request. getrequestcontent () <"quantity" <request. getnumber () <"approved" <Endl; // console. writeline ("{0 }:{ 1} quantity {2} approved", name, request. requestcontent, request. number);} else if (request. getrequesttype () = "salary increase" & request. getnumber () <= 500) {cout <name <request. getrequestcontent () <"quantity" <request. getnumber () <"approved" <Endl; // console. writeline ("{0 }:{ 1} quantity {2} approved", name, request. requestcontent, request. number);} else if (request. getrequesttype () = "salary increase" & request. getnumber () & gt; 500) {cout <name <request. getrequestcontent () <"quantity" <request. getnumber () <"approved" <Endl; // console. writeline ("{0 }:{ 1} count {2}", name, request. requestcontent, request. number) ;}}}; int main () {manager * jinli = new manager ("Jin Li"); Manager * zongjian = new manager ("Zong Jian "); manager * zhongjingli = new manager ("Zhong jingli"); Request * request = new request (); Request-> setrequesttype ("salary increase "); request-> setrequestcontent ("小 "); Request-> setnumber (1000); jinli-> getresult (Manager, * request); zongjian-> getresult (chief_inspector, * request); zhongjingli-> getresult (President, * request); Request * request2 = new request (); request2-> setrequesttype ("leave "); request2-> setrequestcontent ("Food leave"); request2-> setnumber (3); jinli-> getresult (Manager, * request2); zongjian-> getresult (chief_inspector, * request2); zhongjingli-> getresult (President, * request2); Return 0 ;}

2) improved code

Note: class objects cannot be directly declared in C ++, but class pointers can be directly declared.

# Include <iostream> using namespace STD; // Enum managerlevel {"manager", "director", "General Manager"} managerlevel; Enum managerlevel {manager, chief_inspector, President }; // apply for class request {// apply for category PRIVATE: String requesttype; public: String getrequesttype () {return requesttype;} void setrequesttype (string value) {requesttype = value ;} // application content PRIVATE: String requestcontent; public: String getrequestcontent () {retur N requestcontent;} void setrequestcontent (string value) {requestcontent = value;} // number of private: int number; public: int getnumber () {return number ;} void setnumber (INT value) {This-> Number = value ;}}; // manager class manager {protected: string name; // manager * superior; public: manager (string name) {This-> name = Name;} // sets the manager's superior void setsuperior (Manager * Superior) {This-> superior = Superior;} // request virtual void requestapplications (request) {}}; // manager class commonmanager: Public Manager {public: commonmanager (string name): Manager (name) {} void requestapplications (request) {If (request. getrequesttype () = "leave" & request. getnumber () <= 2) {cout <name <request. getrequestcontent () <"quantity" <request. getnumber () <"approved" <Endl;} else {If (Superior! = NULL) Superior-> requestapplications (request) ;}}; // director class majordomo: Public Manager {public: majordomo (string name): Manager (name) {} void requestapplications (request) {If (request. getrequesttype () = "leave" & request. getnumber () <= 5) {cout <name <request. getrequestcontent () <"quantity" <request. getnumber () <"approved" <Endl;} else {If (Superior! = NULL) Superior-> requestapplications (request) ;}}; // general manager class generalmanager: Public Manager {public: generalmanager (string name): Manager (name) {} void requestapplications (request) {If (request. getrequesttype () = "leave") {cout <name <request. getrequestcontent () <"quantity" <request. getnumber () <"approved" <Endl;} else if (request. getrequesttype () = "salary increase" & request. getnumber () <= 500) {cout <name <request. getrequestcontent () <"quantity" <request. getnumber () <"approved" <Endl;} else if (request. getrequesttype () = "salary increase" & request. getnumber () & gt; 500) {cout <name <request. getrequestcontent () <"quantity" <request. getnumber () <"Let's talk about it" <Endl ;}}; int main () {commonmanager * jinli = new commonmanager ("Jin Li "); majordomo * zongjian = new majordomo ("zongjian"); generalmanager * zhongjingli = new generalmanager ("Zhong jingli"); jinli-> setsuperior (zongjian ); zongjian-> setsuperior (zhongjingli); Request * request = new request (); Request-> setrequesttype (""); Request-> setrequestcontent (" "); request-> setnumber (1); jinli-> requestapplications (* request); Request * request2 = new request (); request2-> setrequesttype ("leave "); request2-> setrequestcontent ("Food leave"); request2-> setnumber (4); jinli-> requestapplications (* request2); Request * request3 = new request (); request3-> setrequesttype ("salary increase"); request3-> setrequestcontent ("food request salary increase"); request3-> setnumber (500); jinli-> requestapplications (* request3 ); request * request4 = new request (); request4-> setrequesttype ("salary increase"); request4-> setrequestcontent ("food request salary increase"); request4-> setnumber (1000 ); jinli-> requestapplications (* request4); Return 0 ;}

 

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.