以下是根据文章内容生成的Java责任链设计模式的代码示例,包含两种不同的使用场景:
场景一:请求过滤
- 抽象处理器
java">package com.design.mode.responsibilityChain.responsibility01;
public interface MyFilter {boolean doFilter(MyRequest request, MyResponse response, MyFilterChain chain);
}
- 具体处理器 - 登录过滤器
java">package com.design.mode.responsibilityChain.responsibility01;
public class LoginFilter implements MyFilter {@Overridepublic boolean doFilter(MyRequest request, MyResponse response, MyFilterChain chain) {String reqMsg = request.getReqMsg();if (!reqMsg.contains("username") ||!reqMsg.contains("password")) {System.out.println("登录失败,请重新登录");return false;}String resMsg = response.getResMsg();resMsg += "--登录过滤器返回响应消息";response.setResMsg(resMsg);return true;}
}
- 具体处理器 - 业务过滤器
java">package com.design.mode.responsibilityChain.responsibility01;
public class BusinessFilter implements MyFilter {@Overridepublic boolean doFilter(MyRequest request, MyResponse response, MyFilterChain chain) {String reqMsg = request.getReqMsg();if (!reqMsg.contains("/filter")) {System.out.println("请求的接口路径不存在");return false;}String resMsg = response.getResMsg();resMsg += "--业务逻辑处理完返回响应消息";response.setResMsg(resMsg);return true;}
}
- 处理器链
java">package com.design.mode.responsibilityChain.responsibility01;
import java.util.ArrayList;
import java.util.List;public class MyFilterChain {private List<MyFilter> filterList = new ArrayList<>();public MyFilterChain addFilter(MyFilter filter) {filterList.add(filter);return this;}public void doFilter(MyRequest request, MyResponse response, MyFilterChain chain) {for (MyFilter filter : filterList) {if (!filter.doFilter(request, response, chain)) {break;}}}
}
- 实体类 - 请求类
java">package com.design.mode.responsibilityChain.responsibility01;
public class MyRequest {private String reqMsg;public String getReqMsg() {return reqMsg;}public void setReqMsg(String reqMsg) {this.reqMsg = reqMsg;}
}
- 实体类 - 响应类
java">package com.design.mode.responsibilityChain.responsibility01;
public class MyResponse {private String resMsg;public String getResMsg() {return resMsg;}public void setResMsg(String resMsg) {this.resMsg = resMsg;}
}
- 测试类
java">package com.design.mode.responsibilityChain.responsibility01;
public class RespChainTest01 {public static void main(String[] args) {String reqMsg = "{username:andy,password:123456,url:\"http://ip:port/filter\"}";MyRequest request = new MyRequest();request.setReqMsg(reqMsg);MyResponse response = new MyResponse();MyFilterChain chain = new MyFilterChain();MyFilter loginFilter = new LoginFilter();MyFilter businessFilter = new BusinessFilter();chain.addFilter(loginFilter).addFilter(businessFilter);chain.doFilter(request, response, chain);String resMsg = response.getResMsg();System.out.println("得到响应结果=======" + resMsg);}
}
场景二:请假审批
- 实体类 - 请假请求类
java">package com.design.mode.responsibilityChain.responsibility02;
public class LeaveRequest {private Integer days;private String reason;public LeaveRequest() {}public Integer getDays() {return days;}public void setDays(Integer days) {this.days = days;}public String getReason() {return reason;}public void setReason(String reason) {this.reason = reason;}@Overridepublic String toString() {return "Leave{" +"days=" + days +", reason='" + reason + '\'' +'}';}
}
- 实体类 - 审批结果类
java">package com.design.mode.responsibilityChain.responsibility02;
public class Result {private boolean isRatify;private String info;public boolean isRatify() {return isRatify;}public void setRatify(boolean ratify) {isRatify = ratify;}public String getInfo() {return info;}public void setInfo(String info) {this.info = info;}@Overridepublic String toString() {return "Result{" +"isRatify=" + isRatify +", info='" + info + '\'' +'}';}
}
- 抽象处理器
java">package com.design.mode.responsibilityChain.responsibility02;
public interface Ratify {Result handle(LeaveRequest request, Result result, MyChain chain);
}
- 具体处理器 - 组长审批
java">package com.design.mode.responsibilityChain.responsibility02;
public class GroupLeader implements Ratify {@Overridepublic Result handle(LeaveRequest request, Result result, MyChain chain) {Integer days = request.getDays();if (days <= 2) {result.setRatify(true);result.setInfo("组长同意,早去早回。");} else {chain.doFilter(request, result, chain);}return result;}
}
- 具体处理器 - 主管审批
java">package com.design.mode.responsibilityChain.responsibility02;
public class DepartmentHead implements Ratify {@Overridepublic Result handle(LeaveRequest request, Result result, MyChain chain) {Integer days = request.getDays();if (days <= 4) {result.setRatify(true);result.setInfo("主管同意,路上注意安全。");} else {chain.doFilter(request, result, chain);}return result;}
}
- 具体处理器 - 经理审批
java">package com.design.mode.responsibilityChain.responsibility02;
public class DepartmentManager implements Ratify {@Overridepublic Result handle(LeaveRequest request, Result result, MyChain chain) {Integer days = request.getDays();if (days <= 7) {result.setRatify(true);result.setInfo("经理同意,回去安心办事。");} else {result.setRatify(false);result.setInfo("经理不同意,时间太久。");chain.doFilter(request, result, chain);}return result;}
}
- 处理器链
java">package com.design.mode.responsibilityChain.responsibility02;
import java.util.ArrayList;
import java.util.List;public class MyChain {private int index = 0;private List<Ratify> filterList = new ArrayList<>();public MyChain addFilter(Ratify ratify) {filterList.add(ratify);return this;}public void doFilter(LeaveRequest request, Result result, MyChain chain) {if (index == filterList.size()) {return;}Ratify ratify = filterList.get(index);index++;ratify.handle(request, result, chain);}
}
- 测试类
java">package com.design.mode.responsibilityChain.responsibility02;
public class RespChainTest02 {public static void main(String[] args) {LeaveRequest request = new LeaveRequest();request.setDays(3);Result result = new Result();MyChain chain = new MyChain();Ratify leader = new GroupLeader();Ratify head = new DepartmentHead();Ratify manager = new DepartmentManager();chain.addFilter(leader).addFilter(head).addFilter(manager);chain.doFilter(request, result, chain);System.out.println("请假结果======" + result);}
}