基本介绍
- 职责链模式, 又叫 责任链模式,为请求创建了一个接收者对象的链。请求发送者和接收者进行解耦。
- 职责链模式通常每个接收者都包含对另一个接收者的引用。如果一个对象不能处理该请求,那么它会把相同的请求传给下一个接收者,依此类推。
- 这种类型的设计模式属于行为型模式
职责链模式的原理类图
对原理类图的说明-即(职责链模式的角色及职责
- Handler : 抽象的处理者, 定义了一个处理请求的接口, 同时含义另外 Handler
- ConcreteHandlerA, B 是具体的处理者, 处理它自己负责的请求, 可以访问它的后继者(即下一个处理者), 如果可以处理当前请求,则处理,否则就将该请求交个 后继者去处理,从而形成一个职责链
- Request ,含义很多属性,表示一个请求
职责链模式的注意事项和细节
- 1) 将请求和处理分开,实现解耦,提高系统的灵活性
- 2) 简化了对象,使对象不需要知道链的结构
- 3) 性能会受到影响,特别是在 链比较长的时候,因此需控制链中最大节点数量,一般通过在 Handler 中设置一个最大节点数量,在 setNext()方法中判断是否已经超过阀值,超过则不允许该链建立,避免出现超长链无意识地破坏系统性能
例子
职责链模式解决 OA 系统采购审批
1) 应用实例要求
编写程序完成学校 OA 系统的采购审批项目:需求
采购员采购教学器材
如果金额 小于等于 5000, 由教学主任审批
如果金额 小于等于 10000, 由院长审批
如果金额 小于等于 30000, 由副校长审批
如果金额 超过 30000 以上,有校长审批
请求类
//请求类
public class PurchaseRequest {
private int type = 0; //请求类型
private float price = 0.0f; //请求金额
private int id = 0;
//构造器
public PurchaseRequest(int type, float price, int id) {
this.type = type;
this.price = price;
this.id = id;
}
public int getType() {
return type;
}
public float getPrice() {
return price;
}
public int getId() {
return id;
}
}
处理抽象类Approver
public abstract class Approver {
Approver approver; //下一个处理者
String name; // 名字
public Approver(String name) {
// TODO Auto-generated constructor stub
this.name = name;
}
//下一个处理者
public void setApprover(Approver approver) {
this.approver = approver;
}
//处理审批请求的方法,得到一个请求, 处理是子类完成,因此该方法做成抽象
public abstract void processRequest(PurchaseRequest purchaseRequest);
}
如果金额 小于等于 5000, 由教学主任审批。DepartmentApprover
public class DepartmentApprover extends Approver {
public DepartmentApprover(String name) {
// TODO Auto-generated constructor stub
super(name);
}
@Override
public void processRequest(PurchaseRequest purchaseRequest) {
// TODO Auto-generated method stub
if(purchaseRequest.getPrice() <= 5000) {
System.out.println(" ÇëÇó±àºÅ id= " + purchaseRequest.getId() + " ±» " + this.name + " ´¦Àí");
}else {
approver.processRequest(purchaseRequest);
}
}
}
如果金额 小于等于 10000, 由院长审批CollegeApprover
public class CollegeApprover extends Approver {
public CollegeApprover(String name) {
// TODO Auto-generated constructor stub
super(name);
}
@Override
public void processRequest(PurchaseRequest purchaseRequest) {
// TODO Auto-generated method stub
if(purchaseRequest.getPrice() < 5000 && purchaseRequest.getPrice() <= 10000) {
System.out.println(" ÇëÇó±àºÅ id= " + purchaseRequest.getId() + " ±» " + this.name + " ´¦Àí");
}else {
approver.processRequest(purchaseRequest);
}
}
}
如果金额 小于等于 30000, 由副校长审批ViceSchoolMasterApprover
public class ViceSchoolMasterApprover extends Approver {
public ViceSchoolMasterApprover(String name) {
// TODO Auto-generated constructor stub
super(name);
}
@Override
public void processRequest(PurchaseRequest purchaseRequest) {
// TODO Auto-generated method stub
if(purchaseRequest.getPrice() < 10000 && purchaseRequest.getPrice() <= 30000) {
System.out.println(" ÇëÇó±àºÅ id= " + purchaseRequest.getId() + " ±» " + this.name + " ´¦Àí");
}else {
approver.processRequest(purchaseRequest);
}
}
}
如果金额 超过 30000 以上,有校长审批
public class SchoolMasterApprover extends Approver {
public SchoolMasterApprover(String name) {
// TODO Auto-generated constructor stub
super(name);
}
@Override
public void processRequest(PurchaseRequest purchaseRequest) {
// TODO Auto-generated method stub
if(purchaseRequest.getPrice() > 30000) {
System.out.println(" ÇëÇó±àºÅ id= " + purchaseRequest.getId() + " ±» " + this.name + " ´¦Àí");
}else {
approver.processRequest(purchaseRequest);
}
}
}
Client
public class Client {
public static void main(String[] args) {
// TODO Auto-generated method stub
//创建一个请求
PurchaseRequest purchaseRequest = new PurchaseRequest(1, 31000, 1);
//创建相关的审批人
DepartmentApprover departmentApprover = new DepartmentApprover("张主任");
CollegeApprover collegeApprover = new CollegeApprover("李院长");
ViceSchoolMasterApprover viceSchoolMasterApprover = new ViceSchoolMasterApprover("王副校");
SchoolMasterApprover schoolMasterApprover = new SchoolMasterApprover("佟校长");
//需要将各个审批级别的下一个设置好 (处理人构成环形: )
departmentApprover.setApprover(collegeApprover);
collegeApprover.setApprover(viceSchoolMasterApprover);
viceSchoolMasterApprover.setApprover(schoolMasterApprover);
schoolMasterApprover.setApprover(departmentApprover);
departmentApprover.processRequest(purchaseRequest);
viceSchoolMasterApprover.processRequest(purchaseRequest);
}
}