责任链模式:将能够处理同一类请求的对象连成一条链,如果不能处理则传递给链上的下一个对象。
//请假对象public class QingjiaRequest { private String name; private int days; private String reason; public QingjiaRequest(String name, int days, String reason) { this.name = name; this.days = days; this.reason = reason; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getDays() { return days; } public void setDays(int days) { this.days = days; } public String getReason() { return reason; } public void setReason(String reason) { this.reason = reason; }}
//抽象类public abstract class Leader { String name; Leader nextLeader; public Leader(String name) { this.name = name; } public void setNextLeader(Leader nextLeader) { this.nextLeader = nextLeader; } //设置责任链的处理对象(核心) public abstract void handleRequest(QingjiaRequest qingjiaRequest);}
//主任public class Zhuren extends Leader{ public Zhuren(String name){ super(name); } public void handleRequest(QingjiaRequest qingjiaRequest) { if(qingjiaRequest.getDays()<3){ System.out.println("员工请假小于3天"); System.out.println("审批通过"); }else{ if(this.nextLeader!=null){ this.nextLeader.handleRequest(qingjiaRequest); } } }}
//经理public class Manager extends Leader{ public Manager(String name){ super(name); } public void handleRequest(QingjiaRequest qingjiaRequest) { if(qingjiaRequest.getDays()<10&&qingjiaRequest.getDays()>3){ System.out.println("经理审批通过"); }else{ //经理处理后的逻辑 } }}
public static void main(String[] args) { QingjiaRequest qingjiaRequest = new QingjiaRequest("张三", 2, "请假"); //QingjiaRequest qingjiaRequest1 = new QingjiaRequest("张三", 5, "请假"); Leader a = new Zhuren("张主任"); Leader b = new Manager("王经理"); //可以用一个容器对象责任链对象进行一些封装 a.setNextLeader(b); //开始请假 a.handleRequest(qingjiaRequest); }
应用场景:try catch,javascript 冒泡,servlet filter,struts2 拦截器
模仿 JavaWeb 中的 Filter,可以对其思路有个很好的把握
1.Filterpackage com.cun.filters;import com.cun.main.FilterChain;import com.cun.main.Request;import com.cun.main.Response;public interface Filter { void doFilter(Request request, Response response, FilterChain chain);} 2.FirstFilterpackage com.cun.filters;import com.cun.main.FilterChain;import com.cun.main.Request;import com.cun.main.Response;public class FirstFilter implements Filter { @Override public void doFilter(Request request, Response response, FilterChain chain) { request.doSomething(this.getClass().getName()); // 先执行所有request再倒序执行所有response chain.doFilter(request, response, chain); response.doSomething(this.getClass().getName()); }}3.SecondFilterpackage com.cun.filters;import com.cun.main.FilterChain;import com.cun.main.Request;import com.cun.main.Response;public class SecondFilter implements Filter { @Override public void doFilter(Request request, Response response, FilterChain chain) { request.doSomething(this.getClass().getName()); chain.doFilter(request, response, chain); response.doSomething(this.getClass().getName()); }} 3.ThirdFilterpackage com.cun.filters;import com.cun.main.FilterChain;import com.cun.main.Request;import com.cun.main.Response;public class ThirdFilter implements Filter { @Override public void doFilter(Request request, Response response, FilterChain chain) { request.doSomething(this.getClass().getName()); chain.doFilter(request, response, chain); response.doSomething(this.getClass().getName()); }}4.Requestpackage com.cun.main;public class Request { public void doSomething(String str) { System.out.println("Request ---> " + str); }}5.Responsepackage com.cun.main;public class Response { public void doSomething(String str) { System.out.println("Response ---> " + str); }} 6.FilterChainpackage com.cun.main;import com.cun.filters.Filter;import java.util.ArrayList;import java.util.List;public class FilterChain implements Filter { private List<Filter> filters = new ArrayList<Filter>(); private int index = 0; // 链式调用 public FilterChain addFilter(Filter filter) { this.filters.add(filter); return this; } @Override public void doFilter(Request request, Response response, FilterChain chain) { if (index == filters.size()) { return; } Filter filter = filters.get(index); index++; filter.doFilter(request, response, chain); }} 7.FilterTest 测试package com.cun.main;import com.cun.filters.FirstFilter;import com.cun.filters.SecondFilter;import com.cun.filters.ThirdFilter;import org.junit.Test;public class FilterTest { @Test public void Test(){ FilterChain filterChain = new FilterChain(); filterChain.addFilter(new FirstFilter()).addFilter(new SecondFilter()).addFilter(new ThirdFilter()); filterChain.doFilter(new Request(),new Response(),filterChain); }}