1, Responsibility chain model
1. What is the responsibility chain model
In the Chain of Responsibility Pattern, each receiver usually contains a reference to another receiver. If an object cannot process the request, it passes the same request to the next recipient, and so on.
Chain of responsibility model: multiple objects have the opportunity to process the request, so as to avoid the amount coupling relationship between the sender and receiver of the request Connect the object into a chain and pass the request along the chain until an object processes it
2. Usage scenario
The implementation of filter chain in Java Web is actually a responsibility chain mode
3. Advantages and disadvantages
advantage:
- Reduce coupling. It decouples the sender and receiver of the request.
- Simplifies objects. So that the object does not need to know the structure of the chain.
- Enhance the flexibility of assigning responsibilities to objects. By changing the members in the chain or transferring their order, it is allowed to dynamically add or delete responsibilities.
- It is convenient to add new request processing classes.
Disadvantages:
- The system performance will be affected to some extent, and it is inconvenient to debug the code, which may cause circular call.
- It may not be easy to observe the characteristics of the runtime, which hinders debugging.
2, The simplest implementation
At present, there is a data that needs multiple checks: timeliness test, risk control interception test, frequency limit test, etc. each check is separated from each other into a separate class. Its implementation code is as follows:
1. Define Filter interface
package com.tang.chain.handler; public interface Filter { /** * Filter check * * @param data */ void handle(String data); /** * Set next * * @param filter */ void setNext(Filter filter); /** * Get next * * @return */ Filter next(); }
2. Write three corresponding check and filter implementation classes
import com.tang.chain.handler.Filter; public class DurationFilter implements Filter { private Filter nextFilter; public DurationFilter(){ } public DurationFilter(Filter filter){ this.nextFilter=filter; } @Override public void handle(String data) { System.out.println("Timeliness test:"+data); } @Override public void setNext(Filter filter){ this.nextFilter=filter; } @Override public Filter next(){ return nextFilter; } }
import com.tang.chain.handler.Filter; public class RiskFilter implements Filter { private Filter nextFilter; public RiskFilter(){ } public RiskFilter(Filter filter){ this.nextFilter=filter; } @Override public void handle(String data) { System.out.println("Risk control interception inspection:"+data); } @Override public void setNext(Filter filter){ this.nextFilter=filter; } @Override public Filter next(){ return nextFilter; } }
import com.tang.chain.handler.Filter; public class TimesFilter implements Filter { private Filter nextFilter; public TimesFilter(){ } public TimesFilter(Filter filter){ this.nextFilter=filter; } @Override public void handle(String data) { System.out.println("Frequency limit test:"+data); } @Override public void setNext(Filter filter){ this.nextFilter=filter; } @Override public Filter next(){ return nextFilter; } }
3. Write unit tests
import com.tang.chain.handler.Filter; import com.tang.chain.handler.impl.DurationFilter; import com.tang.chain.handler.impl.RiskFilter; import com.tang.chain.handler.impl.TimesFilter; public class ChainApplication { public static void test01() { String data = "hello world!"; //Manual assembly responsibility chain Filter fiterChain = new DurationFilter(new RiskFilter(new TimesFilter(null))); while (null != fiterChain) { fiterChain.handle(data); fiterChain = fiterChain.next(); } } public static void test02() { String data = "hello world!"; Filter durationFilter = new DurationFilter(); Filter riskFilter = new RiskFilter(); Filter timesFilter = new TimesFilter(); //Manual assembly responsibility chain durationFilter.setNext(riskFilter); riskFilter.setNext(timesFilter); timesFilter.setNext(null); Filter rootFilter = durationFilter; while (null != rootFilter) { rootFilter.handle(data); rootFilter = rootFilter.next(); } } public static void main(String[] args) { test02(); } }
3, Summary
The above is the basic usage of the responsibility chain model Responsibility chain is also called responsibility chain. Each responsibility can be defined as a processing object Using the responsibility chain pattern can help us realize non mutually exclusive multi if judgment in business code more gracefully It's also easier when you need to expand
The above is only the responsibility chain configured manually through coding. Of course, it can also be configured automatically with the help of the IOC container in the SpringBoot framework. Refer to: https://www.heyinglei.com/archives/java%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F-%E8%B4%A3%E4%BB%BB%E9%93%BE%E6%A8%A1%E5%BC%8F