12.责任链模式(设计模式笔记)
来源:互联网 发布:国外网络代理 编辑:程序博客网 时间:2024/05/20 09:43
行为型模式关注系统中对象之间的相互交互,研究系统在运行时对象之间的相互通信和协作,
进一步明确对象的职责
创建型模式关注对象的创建过程
进一步明确对象的职责
创建型模式关注对象的创建过程
结构型模式关注对象和类的组织
定义:
将能够处理同一类请求的对象连城一条链,所提交的请求沿着链传递,
链上的对象逐个判断是否有能力处理该请求,如果能则处理,如果不能则
传递给链上的下一个对象。
场景:
打牌时,轮流出牌
接力赛跑
大学中,奖学金审批
公司中,公文审批
开发中常见的场景:
-java中,异常机制就是一种责任链模式。一个try可以对应多个catch,
当第一个catch不匹配类型,则自动跳到第二个catch。
-javascript语言中,事件的冒泡和捕获机制。java语音中,事件的处理采用观察者模式。
-Servlet开发中,过滤器的链式处理
-struts2中,拦截器的调用也是典型的责任链模式。
/** * 封装请假的基本信息 * @author Administrator * */public class LeaveRequest {private String empName;private int leaveDays;private String reason;public LeaveRequest(String empName, int leaveDays, String reason) {super();this.empName = empName;this.leaveDays = leaveDays;this.reason = reason;}public String getEmpName() {return empName;}public void setEmpName(String empName) {this.empName = empName;}public int getLeaveDays() {return leaveDays;}public void setLeaveDays(int leaveDays) {this.leaveDays = leaveDays;}public String getReason() {return reason;}public void setReason(String reason) {this.reason = reason;}}
/** * 抽象类 * @author Administrator * */public abstract class Leader {protected String name;protected Leader nextLeader; //责任链下一级对象public Leader(String name) {super();this.name = name;}//设置责任链的后续对象public void setNextLeader(Leader nextLeader) {this.nextLeader = nextLeader;}/** * 处理请求的核心的业务方法 * @param request */public abstract void handleRequest(LeaveRequest request);}
/** * 主任(审批请假天数小于三天) * @author Administrator * */public class Director extends Leader{public Director(String name) {super(name);}@Overridepublic void handleRequest(LeaveRequest request) {if(request.getLeaveDays() < 3) {System.out.println("主任"+this.name+"审批-" + "员工"+request.getEmpName()+"请假"+request.getLeaveDays()+"天,理由:" + request.getReason());} else {if(this.nextLeader != null) {this.nextLeader.handleRequest(request);}}}}
/** * 经理(审批请假天数小于10天大于三天) * @author Administrator * */public class Manager extends Leader{public Manager(String name) {super(name);}@Overridepublic void handleRequest(LeaveRequest request) {if(request.getLeaveDays() < 10 && request.getLeaveDays() > 3) {System.out.println("经理"+this.name+"审批-" + "员工"+request.getEmpName()+"请假"+request.getLeaveDays()+"天,理由:" + request.getReason());} else {if(this.nextLeader != null) {this.nextLeader.handleRequest(request);}}}}
/** * 总经理(审批请假天数大于) * @author Administrator * */public class GeneralManager extends Leader{public GeneralManager(String name) {super(name);}@Overridepublic void handleRequest(LeaveRequest request) {if(request.getLeaveDays() > 10 && request.getLeaveDays() < 30) {System.out.println("总经理"+this.name+"审批-" + "员工"+request.getEmpName()+"请假"+request.getLeaveDays()+"天,理由:" + request.getReason());} else {System.out.println("莫非:" + request.getEmpName() + "想辞职,居然请假"+request.getLeaveDays() + "天");}}}
public class Client {public static void main(String[] args) {Leader a = new Director("张三");Leader b = new Manager("李四");Leader c = new GeneralManager("王五");//组织责任链对象之间的关系a.setNextLeader(b);b.setNextLeader(c);//开始请假操作LeaveRequest req1 = new LeaveRequest("甜瓜", 200, "想去旅游");a.handleRequest(req1);}}
0 0
- 12.责任链模式(设计模式笔记)
- 设计模式笔记-责任链模式
- 设计模式笔记--责任链模式
- Android 设计模式 笔记 - 责任链模式
- 设计模式学习笔记--责任链模式
- 设计模式笔记十四:责任链模式
- 69.设计模式笔记-责任链模式
- 责任链设计模式
- 责任链设计模式
- 设计模式---责任链
- 责任链设计模式
- 设计模式----责任链
- 设计模式-责任链
- 责任链设计模式
- 责任链设计模式
- 设计模式:责任链
- [设计]责任链模式
- 设计模式--责任链
- ubuntu15.04下安装jdk8
- Javascript高级程序设计第十二章
- WEB前端-IE6,IE7,IE8,IE9,Firefox兼容性问题
- 如何删除字符串中重复的字符
- eclipse设置字体大小
- 12.责任链模式(设计模式笔记)
- javascript 之this及作用域整理
- 小知识点(11)微信JS SDK 验证和简单使用
- 嵌入式linux之IIS驱动
- 使用ViewPager实现左右循环滑动
- POJ Matrix ( 二维树状数组)
- Linux命令 uniq
- mysql cluster管理节点配置详解
- LeetCode3.2(Implement strStr())