设计模式系列之二_责任链模式(Chain of Responsibility)
来源:互联网 发布:esa white 知乎 编辑:程序博客网 时间:2024/06/07 05:54
1.职责链模式定义
为了避免请求的发送者和接收者之间的耦合关系,使多个接受对象都有机会处理请求。将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。
2.Chain of Responsibility模式主要涉及两个角色
1)抽象处理者角色(Handler):它定义了一个处理请求的接口。当然对于链子的不同实现,也可以在这个角色中实现后继链。
2)具体处理者角色(ConcreteHandler):实现抽象角色中定义的接口,并处理它所负责的请求。
如果不能处理则访问它的后继者。
如下图:
3.职责链模式应用--请假管理
假设请假的流程如下:1.在公司里,如果你的请假时间小于0.5天,那么只需要向项目经理打声招呼就OK了。
2.如果超过了0.5天,但是还小于2天,那么就要去找人事部处理,当然,这就要扣工资了。
3.如果超过了2天,你就需要去找总经理了,工资当然也玩完了。
4.Java代码实现
如下:
/** * @Title:请假请求类 * @Description:TODO * @Company: Orclight's Studio * @author: shuzl 2013-3-12 上午09:40:13 * @motto: Never put off what you can do today until Tomorrow * @version 1.0.0 */public class Request {private float day;private String reason;public Request() {}public Request(int day,String reason) {this.day=day;this.reason=reason;}public float getDay() {return day;}public void setDay(float day) {this.day = day;}public String getReason() {return reason;}public void setReason(String reason) {this.reason = reason;}}
/** * @Title:请假处理接口 * @Description:TODO * @Company: Orclight's Studio * @author: shuzl 2013-3-12 上午09:43:25 * @motto: Never put off what you can do today until Tomorrow * @version 1.0.0 */public interface ProcessRequest{public boolean handle(Request request);}
/** * @Title:项目经理--实现请假接口 * @Description:TODO * @Company: Orclight's Studio * @author: shuzl 2013-3-12 上午09:44:38 * @motto: Never put off what you can do today until Tomorrow * @version 1.0.0 */public class ProjectManager implements ProcessRequest{private ProcessRequest boss;public boolean handle(Request request) {float day=request.getDay();if(day<=0.5) {System.out.println("PM 已经批准处理成功!请假原因:"+request.getReason());return true;}return boss.handle(request);}public ProcessRequest getBoss() {return boss;}public void setBoss(ProcessRequest boss) {this.boss = boss;}}
/** * @Title:项目经理--实现请假接口 * @Description:TODO * @Company: Orclight's Studio * @author: shuzl 2013-3-12 上午09:47:39 * @motto: Never put off what you can do today until Tomorrow * @version 1.0.0 */public class HR implements ProcessRequest{private ProcessRequest boss;@Overridepublic boolean handle(Request request) {float day = request.getDay();if(day>0.5&&day<=2) {System.out.println("HR已经批准,处理成功!请假原因:"+request.getReason());return true;} return boss.handle(request);}public ProcessRequest getBoss() {return boss;}public void setBoss(ProcessRequest boss) {this.boss = boss;}}
/** * @Title:经理--实现请假接口 * @Description:TODO * @Company: Orclight's Studio * @author: shuzl 2013-3-12 上午09:50:38 * @motto: Never put off what you can do today until Tomorrow * @version 1.0.0 */public class Manager implements ProcessRequest{private ProcessRequest boss;@Overridepublic boolean handle(Request request) {float day=request.getDay();if(day>2) {System.out.println("总经理审批通过!请假原因:"+request.getReason());}return false;}public ProcessRequest getBoss() {return boss;}public void setBoss(ProcessRequest boss) {this.boss = boss;}}
/** * @Title:请假流程测试类 * @Description:TODO * @Company: Orclight's Studio * @author: shuzl 2013-3-12 上午09:53:24 * @motto: Never put off what you can do today until Tomorrow * @version 1.0.0 */public class TestRequest {public static void main(String[] args) {//1.发出请求Request request = new Request(3,"病假");//2.创建处理对象ProjectManager pm = new ProjectManager();HR hr = new HR();Manager manager = new Manager();//3.设置chain的流向,关联关系pm.setBoss(hr);hr.setBoss(manager);//4.处理请求pm.handle(request);}}
参考文章:
1.设计模式学习笔记(十九)—Chain of Responsibility职责链模式
2.重温设计模式(三)——职责链模式(chain of responsibility)
- 设计模式系列之二_责任链模式(Chain of Responsibility)
- 设计模式之二十一------ chain of responsibility(责任链)
- 设计模式_责任链模式(Chain Of Responsibility)
- 设计模式之责任链Chain of Responsibility模式
- 设计模式之责任链模式(Chain of Responsibility)
- 设计模式之---责任链模式(Chain of Responsibility)
- 设计模式之责任链模式--- Pattern chain-of-responsibility
- 设计模式之13 - 责任链模式Chain Of Responsibility
- 设计模式拾荒之责任链模式 ( Chain of Responsibility)
- 设计模式之责任链模式(Chain of Responsibility)
- 设计模式之责任链(Chain of Responsibility)
- 设计模式之-责任链(Chain of responsibility )
- 设计模式之责任链Chain of Responsibility
- (三)二十三中设计模式之我见-------------责任链模式(Chain of Responsibility)
- 设计模式之二十:责任链模式(Chain of Responsibility)
- 【设计模式】之二Chain of Responsibility
- 设计模式 - Chain of Responsibility Pattern(责任链模式)
- 设计模式 - Chain of Responsibility Pattern(责任链模式)
- UI -- IOS6屏幕旋转问题总结
- [php] Realize Php
- B树、B-树、B+树、B*树
- 跟我一起写 Makefile
- 装载问题
- 设计模式系列之二_责任链模式(Chain of Responsibility)
- C++编程思想读书笔记之 第三章 隐藏实现
- httpclient4.2.2的几个常用方法,登录之后访问页面问题,下载文件
- 一些C语法回顾
- UI -- CorePloty应用指南
- 关于qt的一些小东东
- oracle创建表空间
- SVN commit:remains in tree-conflict错误的解决办法
- float, int , int64计算性能