[responsibility chain mode of design mode]

1. Definition: make multiple objects have the opportunity to process the request, so as to avoid the coupling relationship between the sender and the receiver of the request, connect the object into a chain, and pass the request along the chain until one object processes it.

2. Implementation code:

Chain interface

/**
 * @Author: Liu Yue
 * @Descripition:
 * @Date; Create in 2020/3/14 22:23
 **/
public interface Chain {
    void next();
}
/**
 * @Author: Liu Yue
 * @Descripition:
 * @Date; Create in 2020/3/16 8:53
 **/
public class Member {
}

ChainHandler

/**
 * @Author: Liu Yue
 * @Descripition:
 * @Date; Create in 2020/3/14 22:29
 **/
public abstract class ChainHandler<T> {

    protected ChainHandler next;

    public void next(ChainHandler next) {
        this.next = next;
    }

    public abstract void doHandler(Member member);

    public static class Builder<T> {
        private ChainHandler head = null;
        private ChainHandler tail = null;

        public Builder<T> addHandler(ChainHandler chain) {
            if (this.head == null) {
                this.head = this.tail = chain;
                return this;
            }
            this.tail.next(chain);
            this.tail = chain;
            return this;
        }

        public ChainHandler<T> build() {
            return this.head;
        }
    }
}

Chain implementation class

/**
 * @Author: Liu Yue
 * @Descripition:
 * @Date; Create in 2020/3/14 22:26
 **/
public class FirstChain extends ChainHandler {

    @Override
    public void doHandler(Member member) {
        System.out.println("First pass");
        next.doHandler(member);
    }
}
public class SecondChain extends ChainHandler {

    @Override
    public void doHandler(Member member) {
        System.out.println("The second gate");
        next.doHandler(member);
    }
}
public class ThirdChain extends ChainHandler {

    @Override
    public void doHandler(Member member) {
        System.out.println("The third pass");
        if (member != null) {
            next.doHandler(member);
        }
    }
}

Client test class

/**
 * @Author: Liu Yue
 * @Descripition:
 * @Date; Create in 2020/3/16 8:49
 **/
public class ChainTest1 {

    public void chain(Member member) {
        ChainHandler.Builder builder = new ChainHandler.Builder();
        builder.addHandler(new FirstChain())
                .addHandler(new SecondChain())
                .addHandler(new ThirdChain());

        builder.build().doHandler(member);

    }

    public static void main(String[] args) {
        ChainTest1 chainTest1 = new ChainTest1();
        chainTest1.chain(new Member());
    }
}

3. filter in servlet

public final class ApplicationFilterChain implements FilterChain {
    private int pos = 0; //offset of currently executing filter
    private int n; //Number of current filter s
    private ApplicationFilterConfig[] filters;  //Filter configuration class, get filter through getFilter() method
    private Servlet servlet
  
    @Override
    public void doFilter(ServletRequest request, ServletResponse response) {
        if (pos < n) {
            ApplicationFilterConfig filterConfig = filters[pos++];
            Filter filter = filterConfig.getFilter();
            filter.doFilter(request, response, this);
        } else {
            // After all the filter s are processed, execute the servlet
            servlet.service(request, response);
        }
    }
}

4. filter in dubbo

Dubbo is another method when creating a Filter. It encapsulates the Filter as an anonymous class of Invoker and completes the responsibility chain through a data structure such as a linked list.

In Dubbo's responsibility chain, there is no class like FilterChain, which combines Filter with invoking Invoker. Instead, by creating a chain list, we only know the first node when invoking, and each node contains the node information of the next call. Although the Invoker encapsulates the specified next that the Filter does not display, it achieves the same effect through the mechanism of java anonymous class and final.

5. plugin in mybatis

Mybatis can configure all kinds of plugins, whether officially provided or defined by itself. Like Filter, plugins perform some operations when executing Sql statements. The responsibility chain of mybatis uses Plugin to proxy the actual Executor class through dynamic proxy. (the composition mode is also used here, because Plugin can nest agents). The core code is as follows:

public class Plugin implements InvocationHandler{
    private Object target;
    private Interceptor interceptor;
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {      
        if (Meet agency conditions) {
            return interceptor.intercept(new Invocation(target, method, args));
        }
        return method.invoke(target, args);     
    }
   
    //Proxy the incoming object, either the actual Executor class or the Plugin proxy class
    public static Object wrap(Object target, Interceptor interceptor) {
  
        Class<?> type = target.getClass();
        Class<?>[] interfaces = getAllInterfaces(type, signatureMap);
        if (interfaces.length > 0) {
            return Proxy.newProxyInstance(
                    type.getClassLoader(),
                    interfaces,
                    new Plugin(target, interceptor, signatureMap));
        }
        return target;
    }
} 

Thank you https://www.cnblogs.com/z-test/p/9319116.html  

Improve a little every day

Tags: Programming Dubbo Mybatis Java SQL

Posted on Mon, 16 Mar 2020 02:56:46 -0400 by mamavi