describe
  • define

Chain of Responsibility: Avoid coupling the sender of the request with the receiver, make it possible for more than one object to be cleared, link these objects into a Chain, and pass the request along the Chain until there is an object to process.

  • Advantages 1) make an object does not need to know by which an object handle the request, and only know the request will be processed object, the receiver and the sender don’t have the clear information of each other, and don’t need to know the structure of the chain of objects in the chain, the client shall be responsible for the chain to create, reduce the coupling of 2) request processing system object only needs to maintain a reference to the chain, 3) The chain can be dynamically added and modified to handle a request. 4) A new concrete handler can be added to the system without modifying the original system code, just rechain the rules on the client side

  • disadvantages

Recipient 1) request is not clear, cannot guarantee that this request will be processed, the request may until the end of the chain are not process 2) for a relatively long chain of responsibility, the request object may involve more than one process, system performance will be affected by a 3) create chain is undeserved, may cause circulation call, will cause the system into a infinite loop

Case description

In the blog message system, after the server receives the message string submitted by the client, to prevent the HTML tag submitted by the client from affecting the normal display of the website or malicious injection code from affecting the normal operation of the server, the submitted string needs to be chain filtered. And at the time of return, the data will be filtered and displayed according to the rules of the chain.

Code Implementation (Java)
  • Request and Response objects

    public class Request {
    
    	@Getter
    	@Setter
    	private String requestStr;
    }
    Copy the code
    public class Response {
    
    	@Getter
    	@Setter
    	private String responseStr;
    }
    Copy the code
  • Filter interface :Filter

    public interface Filter {
    	void doFilter(Request request, Response response, FilterChain chain);
    }
    Copy the code
  • Filter chain: Filter chain implementation Filter to facilitate the addition of Filter chain

    public class FilterChain implements Filter { private final List<Filter> filters = new ArrayList<Filter>(); int index = 0; public FilterChain addFilter(Filter filter) { this.filters.add(filter); return this; } public void doFilter(Request request, Response response, FilterChain chain) { if (index == filters.size()) { return; } Filter filter = filters.get(index++); filter.doFilter(request, response, chain); }}Copy the code
  • Filter implementation class HtmlFilter

    public class HtmlFilter implements Filter { public void doFilter(Request request, Response response, FilterChain chain) { request.setRequestStr(request.getRequestStr() + "-HtmlFilter-"); chain.doFilter(request, response, chain); response.setResponseStr(response.getResponseStr() + "-HtmlFilter-"); }}Copy the code

    SesitiveFilter

    public class SesitiveFilter implements Filter { public void doFilter(Request request, Response response, FilterChain chain) { request.setRequestStr(request.getRequestStr() + "-SesitiveFilter-"); chain.doFilter(request, response, chain); response.setResponseStr(response.getResponseStr() + "-SesitiveFilter-"); }}Copy the code

    SqlInjectionFilter

    public class SqlInjectionFilter implements Filter { public void doFilter(Request request, Response response, FilterChain chain) { request.setRequestStr(request.getRequestStr() + "-SqlInjectionFilter-"); chain.doFilter(request, response, chain); response.setResponseStr(response.getResponseStr() + "-SqlInjectionFilter-"); }}Copy the code
  • test

    public static void main(String[] args) {
        FilterChain fc1 = new FilterChain();
        fc1.addFilter(new HtmlFilter()).addFilter(new SesitiveFilter());
    
        FilterChain fc2 = new FilterChain();
        fc2.addFilter(new SqlInjectionFilter());
    
        fc1.addFilter(fc2);
    
        Request request = new Request();
        request.setRequestStr("request");
    
        Response response = new Response();
        response.setResponseStr("response");
    
        fc1.doFilter(request, response, fc1);
    
        System.out.println(request.getRequestStr());
        System.out.println(response.getResponseStr());
    
    }
    Copy the code

    The results of

    request-HtmlFilter--SesitiveFilter--SqlInjectionFilter-
    response-SqlInjectionFilter--SesitiveFilter--HtmlFilter-
    Copy the code