Responsibility chain pattern: multiple objects are connected into a chain by references corresponding to the next home of their objects, and the request is passed on this chain until a receiving object on the chain processes the request. Because the requesting client does not know who will eventually handle the request on the chain, the system can dynamically reorganize and allocate responsibilities without affecting the client, thereby avoiding coupling between the request sender and the request handler.
There are three roles involved in the chain of responsibility:
1. Abstract Processor Role
2. Specific handler role
3. Small example of the request sender: Suppose you go to buy a house, and you need to bargain when buying a house. The positions of the person who sells the house are different, and the discounted prices are also different. Different positions can form a chain for processing the request. We tentatively set: * Grassroots salespersons can only offer 3% discount * Sales Manager: 5% discount * Sales Director: 8% discount * Boss: 10% discount
Java instance
Class diagram:
/** * Abstract responsibility*/ public abstract class IFilter { private IFilter successor; public IFilter getSuccessor() { return successor; } public void setSuccessor(IFilter successor) { this.successor = successor; } public abstract void handleFilter(); public abstract void handleFilter2(); } /** * Specific responsibilities*/ public class ConcreteFilter extends IFilter { private String name; public ConcreteFilter(String name) { this.name = name; } @Override public void handleFilter() { /* * Handle it first, if there is a successor, then handle it once */ System.out.println(name + "Request processed"); if (getSuccessor() != null) { getSuccessor().handleFilter(); } } @Override public void handleFilter2() { /* * If there is a successor, then the successor will be handled by yourself. Otherwise, you will handle it yourself*/ if (getSuccessor() != null) { getSuccessor().handleFilter2(); } else { System.out.println(name + "Request processed"); } } } public class Test { public static void main(String[] args) { IFilter filter1 = new ConcreteFilter("permission-filter");//Permission filter IFilter filter2 = new ConcreteFilter("suffix-filter");//Suffix filter IFilter filter3 = new ConcreteFilter("style-filter");//Style filter1.setSuccessor(filter2); filter2.setSuccessor(filter3); System.out.println("-----The following is handled by every processor (including successors), The order is also a level-by-level pass------"); filter1.handleFilter(); System.out.println("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Print:
------The following is the process of each processor (including successors), and the order is also one level after another----- permission-filter handles the request suffix-filter handles the request style-filter handles the request ------- The following is the process of the last successor---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------