通过请假的例子讲解责任链模式的使用。责任链在工作中也比较常用,如请假,公司申请离职,入职审批流,采购审批等等。
为请求创建了一个接收者对象的链。
职责链上的处理者负责处理请求,客户只需要将请求发送到职责链上即可,无须关心请求的处理细节和请求的传递,所以职责链将请求的发送者和请求的处理者解耦了。
拦截的类都实现统一接口。
以请假为例,请假需要班主任,副校长,校长审批。
/**
* 请假 需要三级审批
* level1 班主任
* level2 副校长
* level3 正校长
*/
public class AskforLeaveRequest {
//请假的姓名
private String stuName;
//请假原因
private String reason;
//审批级别到
private int level = 1;
public AskforLeaveRequest(String stuName, String reason) {
this.stuName = stuName;
this.reason = reason;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getReason() {
return reason;
}
public void setReason(String reason) {
this.reason = reason;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
}
定义审批链对象,审批人,审批意见,提供方法设置下一个审批者,以及处理请求。
public abstract class Approver {
//处理人
Approver approver;
//处理人姓名
String approverName;
//处理人意见 Y N 同意 不同意
String approverOpinion;
//构造函数
public Approver(String approverName, String approverOpinion) {
this.approverName = approverName;
this.approverOpinion = approverOpinion;
}
//同意设置下一个审批人
public abstract void setNextApprover(Approver approver) ;
//处理审批请求
public abstract void proccessRequest(AskforLeaveRequest askforLeaveRequest);
}
班主任:
package behavior.ChainOfResponsibility;
/**
* 班主任审批
*/
public class HeadTeacherApprover extends Approver {
public HeadTeacherApprover(String approverName, String approverOpinion) {
super(approverName, approverOpinion);
}
@Override
public void setNextApprover(Approver approver) {
this.approver = approver;
}
@Override
public void proccessRequest(AskforLeaveRequest askforLeaveRequest) {
System.out.println("请假人:"+askforLeaveRequest.getStuName()+",请假事由:"+askforLeaveRequest.getReason());
System.out.println("当前审批人:"+this.approverName+",审批意见:"+this.approverOpinion);
}
}
副校长:
package behavior.ChainOfResponsibility;
/**
* 副校长
*/
public class VicePresidentApprover extends Approver{
public VicePresidentApprover(String approverName, String approverOpinion) {
super(approverName, approverOpinion);
}
@Override
public void setNextApprover(Approver approver) {
this.approver = approver;
}
@Override
public void proccessRequest(AskforLeaveRequest askforLeaveRequest) {
System.out.println("请假人:"+askforLeaveRequest.getStuName()+",请假事由:"+askforLeaveRequest.getReason());
System.out.println("当前审批人:"+this.approverName+",审批意见:"+this.approverOpinion);
}
}
校长:
package behavior.ChainOfResponsibility;
/**
* 正校长审批
*/
public class PresidentApprover extends Approver{
public PresidentApprover(String approverName, String approverOpinion) {
super(approverName, approverOpinion);
}
@Override
public void setNextApprover(Approver approver) {
this.approver = approver;
}
@Override
public void proccessRequest(AskforLeaveRequest askforLeaveRequest) {
System.out.println("请假人:"+askforLeaveRequest.getStuName()+",请假事由:"+askforLeaveRequest.getReason());
System.out.println("当前审批人:"+this.approverName+",审批意见:"+this.approverOpinion);
}
}
/**
* 测试
*/
public class ChainOfResponseDemo {
public static void main(String[] args) {
//请假人
AskforLeaveRequest askforLeaveRequest = new AskforLeaveRequest("小哈","感冒");
Approver headTeacher = new HeadTeacherApprover("班主任","同意");
Approver vicePresident = new VicePresidentApprover("副校长","同意");
Approver president = new PresidentApprover("校长","同意");
System.out.println("=============================");
headTeacher.proccessRequest(askforLeaveRequest);
headTeacher.setNextApprover(vicePresident);
System.out.println("=============================");
vicePresident.proccessRequest(askforLeaveRequest);
vicePresident.setNextApprover(president);
System.out.println("=============================");
president.proccessRequest(askforLeaveRequest);
}
}
打印输出:
=============================
请假人:小哈,请假事由:感冒
当前审批人:班主任,审批意见:同意
=============================
请假人:小哈,请假事由:感冒
当前审批人:副校长,审批意见:同意
=============================
请假人:小哈,请假事由:感冒
当前审批人:校长,审批意见:同意
责任链模式在审批流,工作流使用比较常用。