package com.test.chain; import java.util.ArrayList; import java.util.List;
public class BizFilterChain { /** * 执行器集合 */ public List<BizFilter> bizFilters; /** * 执行器下标 */ private int pos; public void addFilter(BizFilter bizFilter){ this.bizFilters.add(bizFilter); } public BizFilterChain(){ this.bizFilters = new ArrayList<>(); } public void process(BizRequest request, BizResponse response){
if (bizFilters.size() > pos) {
BizFilter nextBizFilter = this.bizFilters.get(this.pos++);
nextBizFilter.filter(request, response,this);
}
}
}
具体的处理器:
package com.test.chain; public abstract class AbstractFilter implements BizFilter { @Override public void filter(BizRequest request, BizResponse response, BizFilterChain chain) { if (condition(request,response)) { this.doFilter(request, response, chain); }else { //此处为了继续执行下一个处理器,跟dofilter里的process作用一致 chain.process(request,response); } } /** * 条件判断作为是否执行此filter的准入条件,具体子类需要实现 * @param request * @param response * @return */ public abstract boolean condition(BizRequest request, BizResponse response); /** * 子类的具体实现逻辑 * @param request * @param response * @param chain */ public abstract void doFilter(BizRequest request, BizResponse response, BizFilterChain chain); }
public interface BizFilter { /** * 执行方法 * @param request * @param response * @param chain */ void filter(BizRequest request, BizResponse response,BizFilterChain chain); }
/** * 具体的参数传递对象 * @author chenwei * @date 2018/12/17 10:29 * @Description */ public class BizRequest { }
package com.test.chain; /** * 结果返回对象 * @author chenwei * @date 2018/12/17 10:29 * @Description */ public class BizResponse { }
package com.test.chain; /** * @author chenwei * @date 2018/12/17 10:30 * @Description */ public class Demo1Filter extends AbstractFilter { @Override public void doFilter(BizRequest request, BizResponse response,BizFilterChain chain) { System.out.println("demo1 已经处理过"); //执行这个是为了能继续执行下一个filter,如果想直接返回的话,结束执行,则直接return,不执行process方法 chain.process(request, response); } @Override public boolean condition(BizRequest request, BizResponse response) { return true; } }
package com.test.chain; /** * @author chenwei * @date 2018/12/17 10:30 * @Description */ public class Demo2Filter extends AbstractFilter { @Override public boolean condition(BizRequest request, BizResponse response) { return true; } @Override public void doFilter(BizRequest request, BizResponse response,BizFilterChain chain) { System.out.println("demo2 已经处理过"); chain.process(request,response); } }
package com.test.chain; /** * @author chenwei * @date 2018/12/17 10:30 * @Description */ public class Demo3Filter extends AbstractFilter { @Override public boolean condition(BizRequest request, BizResponse response) { return true; } @Override public void doFilter(BizRequest request, BizResponse response,BizFilterChain chain) { System.out.println("demo3 已经处理过"); chain.process(request,response); } }
package com.test.chain; /** * @author chenwei * @date 2018/12/17 10:42 * @Description */ public class BizExecuter { public static void main(String[] args) { Demo1Filter demo1 = new Demo1Filter(); Demo2Filter demo2 = new Demo2Filter(); Demo3Filter demo3 = new Demo3Filter(); BizFilterChain chain = new BizFilterChain(); chain.addFilter(demo1); chain.addFilter(demo2); chain.addFilter(demo3); BizRequest request = new BizRequest(); BizResponse response = new BizResponse(); chain.process(request, response); } }