The Chain of Responsibility pattern is an object's behavior pattern. In the chain of responsibility mode, many objects are connected by each object's reference to its next home to form a chain. The request is passed on this chain until an object on the chain decides to process the request. The client making this request does not know which object on the chain ultimately handles the request, which allows the system to dynamically reorganize and assign responsibilities without affecting the client.
The responsibility chain model is one of the behavioral design models. How to understand the responsibility chain? The responsibility chain can be understood as connecting several objects at the beginning and end. Each node is an object, and each object corresponds to a different processing logic until an object responds to the processing request ends. This model becomes the responsibility chain model.
Can we find a prototype of the responsibility chain model in life? There are many examples of this kind. For example, if you want to buy a house, the first thing is that the salesperson (application 1) receives you. If you say you want a 3% discount, there is no problem. At this time, the salesperson will have the authority to give a 3% discount, and the salesperson (application 1) will handle it. At this time, a rich man came and said that he wanted to buy 10 units and asked for a 5% discount. The sales staff (application 1) did not have the authority to give a 5% discount. He had to apply to the superior leader, sales director, and sales director (application 2) approved the application. At this time, the national husband Xiao Wang came. Xiao Wang said that he had bought all the properties ten percent. At this time, the sales director (target 2) did not have that much authority and had to apply for approval from the CEO.
In other words, each customer is received by a salesperson. The customer proposes different permissions and the salesperson handes it over to different objects for delivery. The client does not care which object handles his request, which reduces the coupling relationship between the sender and the recipient of the request.
Below, we use a business trip travel expense approval as an example to implement the following responsibility chain model, first defining an abstract leadership class:
package com.test.demo; public abstract class Leader { protected Leader nextHandler;//Previous leadership public final void handlerRequest(int money){ if(money<=limit() ){//Selse{ if(nextHandler!=null){ nextHandler.handlerRequest(money);//Leave it to the previous leadership for processing} } } } /* * Approval limit*/ public abstract int limit(); /* * Approval*/ public abstract void handler(int money); }This is an abstract class. The following inherits it through several classes. First, the group leader class:
package com.test.demo; public class GroupLeader extends Leader { public int limit() { return 1000;//Indicate that the team leader has 1,000 yuan approval authority} public void handler(int money) { System.out.println("The team leader approved"+money); } }Supervisor category:
package com.test.demo; public class Director extends Leader { @Override public int limit() { return 5000; } @Override public void handler(int money) { System.out.println("The supervisor approved"+money); } }Manager category:
package com.test.demo; public class Manager extends Leader { @Override public int limit() { return 10000; } @Override public void handler(int money) { System.out.println("The manager approved"+money); } }Boss category:
package com.test.demo; public class CEO extends Leader { @Override public int limit() { return Integer.MAX_VALUE; } @Override public void handler(int money) { System.out.println("CEO approved"+money); } }There is no limit to bosses. Here is a definition of a staff member Xiao Zhang applying 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); } }Xiao Zhang applied for three transactions like the team leader. At this time, the operation example is as follows:
We can see that different funds are processed by different objects. Xiao Zhang doesn’t care who handles them. He just needs to find the team leader. This is the characteristic of the responsibility chain model.
This is all for you to introduce the relevant content on the Java responsibility chain design model. I hope it will be helpful to you!