Java and design pattern: responsibility chain pattern

Source: Internet
Author: User
Tags class manager

Java and design pattern: responsibility chain pattern

The responsibility chain model is one of the behavioral design patterns. How can we understand the responsibility chain? A responsibility chain is formed by connecting the beginning and end of several objects. Each node is an object, and each object corresponds to different processing logic until an object responds to the Processing request. This mode becomes the responsibility chain mode.

Can we find the prototype of the responsibility chain model in our life? There are many such examples. For example, if you want to buy a house, the first is that the salesperson (Object 1) receives you, and you say you want a 3-percent discount. No problem, at this time, the salesperson has the permission to make a 3% discount, and the salesperson (Object 1) handles it. At this time, a local tyrant said that he would like to buy 10 sets and a 5% discount. The sales staff (Object 1) did not have the permission to get a 5% discount. He must apply for a higher-level lead and sales director, the sales director (Object 2) approved the application. At this time, the national husband Mr. Wang came, and Mr. Wang said that he had bought all the real estate. At this time, the sales director (Object 2) also did not have such great permissions. He had to apply for approval from the CEO.

That is to say, each customer (Client) is received by the sales staff, and the customer puts forward different permissions, and the sales staff gives different objects for progressive processing. The customer does not care about which object has processed his request, which reduces the coupling relationship between the request sender and the receiver.

In the following example, we use a travel expense approval to implement the following responsibility chain model. First, we define an abstract leadership class:

 

Package com. test. demo; public abstract class Leader {protected Leader nextHandler; // The upper-level Leader public final void handlerRequest (int money) {if (money <= limit () {// less than the limit, you can approve handler (money);} else {if (nextHandler! = Null) {nextHandler. handlerRequest (money); // hand it over to the upper-level lead for processing}/** batch quota */public abstract int limit (); /** batch */public abstract void handler (int money );}
This is an abstract class, which is inherited by several classes. The first class is the group leader class:

 

 

Package com. test. demo; public class GroupLeader extends Leader {public int limit () {return 1000; // indicates that the team lead has the permission to approve the request of 1000 yuan} public void handler (int money) {System. out. println ("approved by the Group Leader" + money );}}

Supervisor:

 

 

Package com. test. demo; public class Director extends Leader {@ Overridepublic int limit () {return 5000 ;}@ Overridepublic void handler (int money) {System. out. println ("approved by the supervisor" + 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 ("approved by the manager" + 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 );}}

The boss class has no upper limit. The following defines a staff member Mr. Zhang's request for travel reimbursement:

 

 

package com.test.demo;public class XiaoZhang {public static void main(String[] args) {GroupLeader groupLeader=new GroupLeader();Director 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(500);groupLeader.handlerRequest(5000);}}
Mr. Zhang has applied for three requests, and the running instance is as follows:

We can see that different payments are handled by different objects. Mr. Zhang does not care who handles the payments. He only needs to contact the team leader. This is the characteristic 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.