Responsibility chain mode of design mode

The responsibility chain mode we usually encounter mainly includes the following scenarios:

  1. Filter in servlet
  2. Filter in zuul
  3. Filter in dubbo
  4. Plugin in mybatis

Here we will only explain the implementation of Filter, mainly to understand the responsibility chain mode.

Filter in Servlet

Related instructions

There are two roles for a filter in a servlet

  • Filter: specific filter
  • filter chain: a manager used to coordinate all filters and ensure that they are executed in order

From the above we can guess:

  • FilterChain needs to manage all filters, that is, to store all filters in a List.
  • FilterChain executes the doFilter method of Filter. After the doFilter of Filter is finished, it executes the doFilter method of FilterChain to form recursive operations.

Specific code

FirstFilter (similar to SecondFilter and ThirdFilter)

/**
 * First
 * @author GaoYuan
 * @date 2018/11/11 1:28 p.m.
 */
public class FirstFilter implements IFilter {

    @Override
    public void init() {

    }

    @Override
    public void doFilter(MyRequest myRequest, MyResponse myResponse, IFilterChain chain) {
        System.out.println("\n>>>>>>> Implemented FirstFilter.doFilter()");
        chain.doFilter(myRequest, myResponse);
    }

    @Override
    public void destroy() {

    }
}

FilterChain

/**
 * Filter chain
 * @author GaoYuan
 * @date 2018/11/11 1:28 p.m.
 */
public class FilterChain implements IFilterChain{

    /** Store all filter s */
    private List<IFilter> filterList = new ArrayList<>();
    /** Subscript index of the currently executing filter */
    private int index = 0;

    public FilterChain(){
        // Constructor, calling initialization
        init();
    }

    /**
     * Initialize three filters
     * Of course, you can write a method to provide the adding entry of custom filter
     */
    public FilterChain init(){
        filterList.add(new FirstFilter());
        filterList.add(new SecondFilter());
        filterList.add(new ThirdFilter());
        index = 0;
        return this;
    }

    /**
     * implement
     */
    @Override
    public void doFilter(MyRequest myRequest, MyResponse myResponse){
        if(filterList.size() > index){
            filterList.get(index ++).doFilter(myRequest, myResponse, this);
        }
    }

}

test method

/**
 * There are three filter s in total
 * @author GaoYuan
 * @date 2018/11/11 1:44 p.m.
 */
public static void main(String[] args){
    IFilterChain chain = new FilterChain();
    chain.doFilter(new MyRequest(1), new MyResponse());
}

output

>>>>>>> Implemented FirstFilter.doFilter()

>>>>>>> Implemented SecondFilter.doFilter()

>>>>>>> Implemented ThirdFilter.doFilter()

So far, the filter link takes effect.

Note: for the realization of the other several responsibility chain modes, we will supplement them later.

Code cloud

https://gitee.com/gmarshal/foruo-learn-java/tree/master/src/main/java/com/foruo/learn/designmode/chain

Blog

https://my.oschina.net/gmarshal/blog/2874855

Welcome to my personal wechat subscription number: (it is said that this head portrait app is dedicated to apes)

Keywords: Programming Java Dubbo Mybatis

Added by mbdonner on Sun, 08 Dec 2019 23:55:34 +0200