Java and Design Patterns-the responsibility chain model

Source: Internet
Author: User
Tags class manager

The responsibility chain model belongs to one of the behavioral design patterns, how to understand the responsibility chain? The chain of responsibility can be understood as a number of objects connected by end to end, each node is an object. Each object corresponds to a different processing logic until there is an object response processing request ending. This model becomes the responsibility chain pattern.

Can you find a prototype of a chain of responsibility patterns in your life? There are many examples of this. For example, you are going to buy a house. First, the salesperson (object 1) receives you. You said you wanted a 3 discount, no problem. At this point the salesperson has 3% discount privileges, and the salesperson (object 1) handles it. Then came a local tyrants. Said to buy 10 sets, to 5% discount. The salesperson (object 1) does not have 5% discount permissions. Must want to the superior leader, the Sales Director application, the Sales Director (object 2) approved the application. At this time the national husband Xiao Wang came, Xiao Wang said 10% to buy all the real estate. At this point the Sales Director (Object 2) also did not have that much authority. To apply for an approval from the CEO.

That is, each client (client) is received by the sales staff, the customer proposed different permissions, by the sales staff to the different objects to be handed over. The customer does not care which object handles his request, which reduces the coupling between the requesting sender and the recipient.

Below we use a travel travel approval as an example to implement the following chain of responsibility model. First, define an abstract leader class:

Package Com.test.demo;public abstract class Leader {protected Leader nexthandler;//upper level leader public final void Handlerrequest (int money) {if (Money<=limit ()) {//less than limit, be able to reply handler (money);} Else{if (nexthandler!=null) {nexthandler.handlerrequest (money);//to the upper-level leader to process}}}/* * appropriation limit */public abstract int limit (); * * Appropriation */public abstract void handler (int money);}
This is an abstract class. The following inherit it through several classes, first the team leader class:

Package Com.test.demo;public class Groupleader extends Leader {public int limit () {return 1000;//Description team leader has 1000-yuan reply permission}public void handler (int money) {System.out.println ("team leader approved" +money);}}

Supervisor Class:

Package Com.test.demo;public class Director extends Leader {@Overridepublic int limit () {return 5000;} @Overridepublic void Handler (int money) {System.out.println ("Supervisor approved" +money);}

Manager class:

Package Com.test.demo;public class Manager extends Leader {@Overridepublic int limit () {return 10000;} @Overridepublic void Handler (int money) {System.out.println ("manager approved" +money);}

Boss class:

Package Com.test.demo;public class CEO extends Leader {@Overridepublic int limit () {return integer.max_value;} @Overridepublic void Handler (int money) {System.out.println ("CEO approved" +money);}}

Boss class has no upper limit. To define a staff sheet to apply for travel reimbursement:

Package Com.test.demo;public class Xiaozhang {public static void main (string[] args) {Groupleader groupleader=new grouple Ader ();D irector director=new Director (); Manager manager=new Manager (); CEO Ceo=new CEO (); Groupleader.nexthandler=director;director.nexthandler=manager;manager.nexthandler=ceo; Groupleader.handlerrequest (50000); groupleader.handlerrequest; groupleader.handlerrequest (5000);}}
Xiao Zhang, like team leader, applied for three, at this time the actual proportion of execution is as follows:

We were able to see that different sums were dealt with by different objects. Xiao Zhang doesn't care who's dealing with it, he just needs to find team leader. This is the characteristic of the model of responsibility chain.

Like the attention of friends, thank you



Java and Design Patterns-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.