/**
* 请假条
* 封装请假的信息。
*/
package com.bjsxt.cn.chainOfResponsibility;
public class LeaveRequest {
private String empName;
private int leaveDays;
private String reasons;
public LeaveRequest(String empName, int leaveDays, String reasons) {
super();
this.empName = empName;
this.leaveDays = leaveDays;
this.reasons = reasons;
}
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 getReasons() {
return reasons;
}
public void setReasons(String reasons) {
this.reasons = reasons;
}
@Override
public String toString() {
return "雇员:" + empName + "请假,天数:" + leaveDays + ",请假理由:" + reasons;
}
}
/**
* 抽象类Leader,在请假的流程之中,把审批人员
* 主任,经理,总经理,抽象成一个类,他们
* 具有相同的功能,审批。handleRequest
* 2015年4月9日14:40:49
*
*/
package com.bjsxt.cn.chainOfResponsibility;
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;
}
public abstract void handleRequest(LeaveRequest request);
public abstract String check();
}
/**
* 主任类:
* 如果请假天数小于3天,主任审批
* 2015年4月9日14:43:36
*/
package com.bjsxt.cn.chainOfResponsibility;
public class Director extends Leader {
public Director(String name) {
super(name);
}
@Override
public void handleRequest(LeaveRequest request) {
if (request.getLeaveDays() < 3) {
System.out.println("雇员名字:" + request.getEmpName() +"请假,天数为:" + request.getLeaveDays() +"请假原因为" +request.getReasons());
System.out.println(check());
} else {
if (this.nextLeader != null) {
this.nextLeader.handleRequest(request);
}
}
}
@Override
public String check() {
return "主任:" + name + "审批通过!";
}
}
/**
* 时间:2015年4月9日14:48:13
* 经理类: 如果请假条数大于等于3天,小于十天,经理审批
*/
package com.bjsxt.cn.chainOfResponsibility;
public class Manager extends Leader {
public Manager(String name) {
super(name);
}
@Override
public void handleRequest(LeaveRequest request) {
if (request.getLeaveDays() <10) {
System.out.println(request.toString());
System.out.println(check());
} else {
if (this.nextLeader != null) {
this.nextLeader.handleRequest(request);
}
}
}
@Override
public String check() {
return "经理:" + name + "审批通过";
}
}
/**
* 总经理: 如果大于等于十天,小于三十天,总经理审批
* 时间:2015年4月9日14:53:41
*/
package com.bjsxt.cn.chainOfResponsibility;
public class GeneralManager extends Leader {
public GeneralManager(String name) {
super(name);
}
@Override
public void handleRequest(LeaveRequest request) {
if (request.getLeaveDays() < 30) {
System.out.println(request.toString());
System.out.println(check());
} else {
System.out.println(request.toString() + "莫非想辞职,不批准");
}
}
@Override
public String check() {
return "总经理 :" + name +"审核通过";
}
}
/**
* 2015年4月9日14:59:50
* 测试程序:测试责任链模式的使用
* 我们使用责任链模式而不是非常繁冗复杂的ifelse结构,是为了扩展时的方便。
* 例如,此时,我们如果想要加入一位副总经理,来处理10到20天的假条审核
* 我们只需要加入新的类ViceGeneralManager,在客户程序中创建新的责任链即可。
* 这样符合OCP原则,即开闭原则。
*/
package com.bjsxt.cn.chainOfResponsibility;
public class Client {
public static void main(String[] args) {
LeaveRequest l1 = new LeaveRequest("杰伦", 1, "旅游");
LeaveRequest l2 = new LeaveRequest("张靓颖", 8, "恋爱");
LeaveRequest l3 = new LeaveRequest("夏洛蒂", 15, "失恋了");
LeaveRequest l4 = new LeaveRequest("巴赫", 37, "在家带孩子");
Leader ll1 = new Director("张主任");
Leader ll2 = new Manager("李经理");
Leader ll3 = new GeneralManager("王总经理");
//创建责任链
ll1.setNextLeader(ll2);
ll2.setNextLeader(ll3);
ll1.handleRequest(l1);
ll1.handleRequest(l2);
ll1.handleRequest(l3);
ll1.handleRequest(l4);
}
}
/*
雇员名字:杰伦请假,天数为:1请假原因为旅游
主任:张主任审批通过!
雇员:张靓颖请假,天数:8,请假理由:恋爱
经理:李经理审批通过
雇员:夏洛蒂请假,天数:15,请假理由:失恋了
总经理 :王总经理审核通过
雇员:巴赫请假,天数:37,请假理由:在家带孩子莫非想辞职,不批准
*
*
*/
/**
* 加入副总经理处理十天到二十天的假条审核
* 2015年4月9日15:12:46
*/
package com.bjsxt.cn.chainOfResponsibility;
public class ViceGeneralManager extends Leader{
public ViceGeneralManager(String name) {
super(name);
}
@Override
public void handleRequest(LeaveRequest request) {
if (request.getLeaveDays() < 20) {
System.out.println(request.toString());
System.out.println(check());
} else {
if (this.nextLeader != null) {
this.nextLeader.handleRequest(request);
}
}
}
@Override
public String check() {
return "副总经理:" + name + "审核通过";
}
}
/**
* 增加ViceGeneralManager类之后稍作调整就可以加入加入该类之后的新的责任链。
*/
package com.bjsxt.cn.chainOfResponsibility;
public class Client2 {
public static void main(String[] args) {
LeaveRequest l1 = new LeaveRequest("杰伦", 1, "旅游");
LeaveRequest l2 = new LeaveRequest("张靓颖", 8, "恋爱");
LeaveRequest l3 = new LeaveRequest("夏洛蒂", 15, "失恋了");
LeaveRequest l4 = new LeaveRequest("巴赫", 37, "在家带孩子");
Leader ll1 = new Director("张主任");
Leader ll2 = new Manager("李经理");
Leader ll22 = new ViceGeneralManager("王副总经理");
Leader ll3 = new GeneralManager("王总经理");
//创建责任链
ll1.setNextLeader(ll2);
ll2.setNextLeader(ll22);
ll22.setNextLeader(ll3);
ll1.handleRequest(l1);
ll1.handleRequest(l2);
ll1.handleRequest(l3);
ll1.handleRequest(l4);
}
}
/*
*
雇员名字:杰伦请假,天数为:1请假原因为旅游
主任:张主任审批通过!
雇员:张靓颖请假,天数:8,请假理由:恋爱
经理:李经理审批通过
雇员:夏洛蒂请假,天数:15,请假理由:失恋了
副总经理:王副总经理审核通过
雇员:巴赫请假,天数:37,请假理由:在家带孩子莫非想辞职,不批准
*
* */
GOF23设计模式之责任链模式(chain of responsibility)之实现
原文地址:http://blog.csdn.net/lk142500/article/details/44961085