Responsibility Chain Mode:
Links the recipient object to a chain and passes the request on that chain until a few of the objects handle it. The coupling between the requesting sender and the recipient is avoided by giving more objects the opportunity to process the request.
The advantages and disadvantages of the responsibility chain model:
Advantages: cohesion, low coupling. Business change, add a new level of processing, only need to add a class. Meet our Open/close principle (extended development, change closure).
Cons: Time, memory effect is relatively large. The Java AWT's abandonment of the responsibility chain model embraces the observer pattern.
Application:
Java Exception handling mechanism
JavaScript Event Model
Filter Interceptor for Java EE
Springsecurity Spring Security Framework
Instance:
Change requirements
Instance code:
Class structure diagram
Specific code:
PackageChengxuyuanzhilu.com.cor.handler;/** * @authorPublic Number: Programmer's Road * price processing person, responsible for processing customer discount request*/ Public Abstract classPricehandler {//Direct successor for delivery of requests protectedPricehandler successor; Public voidSetsuccessor (Pricehandler successor) { This. successor =successor; } /** Processing Discount requests*/ Public Abstract voidProcessdiscount (floatdiscount);} PackageChengxuyuanzhilu.com.cor.handler;/** * @authorPublic Number: Programmer's Road * sales, can be approved within 5% discount*/ Public classSalesextendsPricehandler {@Override Public voidProcessdiscount (floatdiscount) { if(Discount <= 0.05) {System.out.format ("%s Approved discount:%2f%n", This. GetClass (). GetName (), discount); }Else{successor.processdiscount (discount); } }} PackageChengxuyuanzhilu.com.cor.handler;/** * @authorPublic Number: Programmer's Road * Business expansion, Management needs subdivision, new and sales team Sales leader * Sales Team leader, can be approved within 15% discount*/ Public classLeadextendssales{@Override Public voidProcessdiscount (floatdiscount) { if(Discount <= 0.15) {System.out.format ("%s Approved discount:%2f%n", This. GetClass (). GetName (), discount); }Else{successor.processdiscount (discount); } }} PackageChengxuyuanzhilu.com.cor.handler;/** * @authorPublic Number: Programmer's Road * Sales Manager, can approve the discount within 30%*/ Public classManagerextendsLead {@Override Public voidProcessdiscount (floatdiscount) { if(Discount <= 0.30) {System.out.format ("%s Approved discount:%2f%n", This. GetClass (). GetName (), discount); }Else{successor.processdiscount (discount); } }} PackageChengxuyuanzhilu.com.cor.handler;/** * @authorPublic Number: Programmer's Road * Sales director, can approve the discount within 40%*/ Public classDirectorextendsManager {@Override Public voidProcessdiscount (floatdiscount) { if(Discount <= 0.40) {System.out.format ("%s Approved discount:%2f%n", This. GetClass (). GetName (), discount); }Else{successor.processdiscount (discount); } }} PackageChengxuyuanzhilu.com.cor.handler;/** * @authorPublic Number: Programmer's Road * VP, can approve the discount within 50%*/ Public classVicePresidentextendsDirector {@Override Public voidProcessdiscount (floatdiscount) { if(Discount <= 0.50) {System.out.format ("%s Approved discount:%2f%n", This. GetClass (). GetName (), discount); }Else{successor.processdiscount (discount); } }} PackageChengxuyuanzhilu.com.cor.handler;/** * @authorPublic Number: Programmer's Road * CEO, can approve the discount within 55%, can reject the discount above 55%*/ Public classCeoextendsManager {@Override Public voidProcessdiscount (floatdiscount) { if(Discount <= 0.55) {System.out.format ("%s Approved discount:%2f%n", This. GetClass (). GetName (), discount); }Else{System.out.format ("%s declined discount:%2f%n", This. GetClass (). GetName (), discount); } }} PackageChengxuyuanzhilu.com.cor.handler;/** * @authorPublic Number: Programmer's Road * Create price processor factory*/ Public classPricehandlefactory {/** Engineering Mode Create price processor*/ Public StaticPricehandler Createpricehandler () {//Upward TransformationPricehandler sales =NewSales (); Pricehandler Lead=NewLead ();//Business ChangePricehandler Manager =NewManager (); Pricehandler Director=NewDirector (); Pricehandler vicepresident=Newvicepresident (); Pricehandler CEO=NewCEO (); //designation of successor sellersSales.setsuccessor (lead); Lead.setsuccessor (manager);//Business ChangeManager.setsuccessor (director); Director.setsuccessor (vicepresident); Vicepresident.setsuccessor (CEO); returnsales; }} PackageChengxuyuanzhilu.com.cor;ImportJava.util.Random;Importchengxuyuanzhilu.com.cor.handler.PriceHandleFactory;ImportChengxuyuanzhilu.com.cor.handler.PriceHandler;/** * @authorPublic Number: Programmer's Road * customer, Request discount*/ Public classCustomer {PrivatePricehandler Pricehandler; Public voidSetpricehandler (Pricehandler pricehandler) { This. Pricehandler =Pricehandler; } /** Send Request*/ Public voidRequestdiscount (floatdiscount) {Pricehandler.processdiscount (discount); } //testing the responsibility chain model Public Static voidMain (string[] args) {Customer customer=NewCustomer (); Customer.setpricehandler (Pricehandlefactory.createpricehandler ()); Random Rand=NewRandom (); for(inti=1;i<100;i++) {System.out.println (i+":"); Customer.requestdiscount (Rand.nextfloat ()); } }}
Java implementation of the responsibility chain model