1#include <iostream>2#include <string>3 4 using namespacestd;5 6 7 classHandler8 {9 Public:TenHandler (handler*Psthandler): M_psthandler (Psthandler) One { A - } - the Virtual voidHandleRequest (intRequest) =0; - - protected: -handler*M_psthandler; + }; - + classConcreteHandler1: PublicHandler A { at Public: -ConcreteHandler1 (handler*Psthandler): Handler (Psthandler) - { - - } - in Virtual voidHandleRequest (intrequest) - { to if(Request <7) + { -cout<<"Group Leader: Granting"<< request<<"days"<<Endl; the } * Else $ {Panax NotoginsengM_psthandler->HandleRequest (request); - } the } + }; A the classConcreteHandler2: PublicHandler + { - Public: $ConcreteHandler2 (handler*Psthandler): Handler (Psthandler) $ { - - } the - Virtual voidHandleRequest (intrequest)Wuyi { the if(Request < the) - { Wucout<<"Director: Granting"<< request<<"days"<<Endl; - } About Else $ { -M_psthandler->HandleRequest (request); - } - } A }; + the classConcreteHandler3: PublicHandler - { $ Public: theConcreteHandler3 (handler*Psthandler): Handler (Psthandler) the { the the } - in Virtual voidHandleRequest (intrequest) the { the if(Request < -) About { thecout<<"Manager: Granting"<< request<<"days"<<Endl; the } the Else + { -cout<<"Manager:"<< request<<"days, the time was too long to exceed the company's rules"<<Endl; the }Bayi } the }; the - intMainintargcChar*argv[]) - { thehandler* Jingli =NewConcreteHandler3 (NULL); thehandler* Zongjian =NewConcreteHandler2 (Jingli); thehandler* Zuzhang =NewConcreteHandler1 (Zongjian); the -Zuzhang->handlerequest ( A); theZuzhang->handlerequest ( the); theZuzhang->handlerequest (5); theZuzhang->handlerequest ( to);94 the DeleteJingli; the DeleteZongjian; the DeleteZuzhang;98 About return 0; - }101 //////////////////////////////////102[Email protected] ~/learn_code/design_pattern/20_chain]$.chain103 Director: Granting 12 days104 Manager: Granting 15 days the Group Leader: granting 5 days106Manager: 31 days, the time is too long, over the company rules
Design pattern--Responsibility Chain mode (c + + implementation)