定义
- 将能够处理同一类请求的对象连成一条链,所提交的请求沿着链传递,链上的对象逐个判断是否高能力处理该请求,如果能则处理,如果不能则传递给链上的下一个对象。
场景
-
打牌时,轮流出牌
-
接力赛跑
-
大学中,奖学金审批
-
公司中,公文审批
-
公司里面,报销个单据需要经过流程:
• 申请人填单申请,申请给经理
• 小于1000,经理审查。
• 超过1000,交给总经理审批。
• 总经理审批通过 -
公司里面,请假条的审批过程:
• 如果请假天数小于3天,主任审批
• 如果请假天数大于等于3天,小于10天,经理审批
• 如果大于等于10天,小于30天,总经理审批
• 如果大于等于30天,提示拒绝
小栗子
package com.principle.chainofresp;
/**
* 请假条的基本信息
*
* @author xy
*
*/
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;
}
}
package com.principle.chainofresp;
/**
* 主任
* @author xy
*
*/
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.getReason());
System.out.println("主任:"+this.name+",审核通过");
}else {
if (this.nextLeader!=null) {
this.nextLeader.handleRequest(request);
}
}
}
}
package com.principle.chainofresp;
/**
* 主任
* @author xy
*
*/
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.getEmpName()+"请假,天数:"+request.getLeaveDays()+",原因:"+request.getReason());
System.out.println("经理:"+this.name+",审核通过");
}else {
if (this.nextLeader!=null) {
this.nextLeader.handleRequest(request);
}
}
}
}
package com.principle.chainofresp;
/**
* 副总经理
* @author xy
*
*/
public class ViceGeneraManager extends Leader{
public ViceGeneraManager(String name) {
super(name);
}
@Override
public void handleRequest(LeaveRequest request) {
if (request.getLeaveDays()<20) {
System.out.println("员工:"+request.getEmpName()+"请假,天数:"+request.getLeaveDays()+",原因:"+request.getReason());
System.out.println("副总经理:"+this.name+",审核通过");
}else {
if (this.nextLeader!=null) {
this.nextLeader.handleRequest(request);
}
}
}
}
package com.principle.chainofresp;
/**
* 主任
* @author xy
*
*/
public class GeneraManager extends Leader{
public GeneraManager(String name) {
super(name);
}
@Override
public void handleRequest(LeaveRequest request) {
if (request.getLeaveDays()<30) {
System.out.println("员工:"+request.getEmpName()+"请假,天数:"+request.getLeaveDays()+",原因:"+request.getReason());
System.out.println("总经理:"+this.name+",审核通过");
}else {
System.out.println("想辞职不干了???");
}
}
}
package com.principle.chainofresp;
public class Client {
public static void main(String[] args) {
Leader a=new Director("张三");
Leader b=new Manager("李四");
Leader c=new ViceGeneraManager("李小四");
Leader d=new GeneraManager("王五");
//组织责任链对象的关系
a.setNextLeader(b);
b.setNextLeader(c);
c.setNextLeader(d);
//开始请假操作
LeaveRequest req1=new LeaveRequest("Tom", 23, "回家探亲");
a.handleRequest(req1);
}
}
控制台打印:
员工:Tom请假,天数:23,原因:回家探亲
总经理:王五,审核通过
网友评论