Java implementation of the responsibility chain model

Source: Internet
Author: User

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

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.