设计模式之职责链模式
1)职责链模式(Chain Of Responsibility Pattern),又叫责任链模式,为请求创建了一个接受者对象的链。这种模式对请求的发送者和接收者进行解耦。
2)职责链模式通常每个接收者都包含另一个接收者的引用。如果一个对象不能处理该请求,那么他会把相同的请求传给下一个接收者,依次类推
3)这种类型的设计模式属于行为型模式
说白了,就是流水线,不是自己整的活就由流下去直到有人做完为止
例子:采购审批流程
金额 小于等于5000 由主任批
金额 小于等于10000由院长批
金额 小于等于30000由副校长批
金额 超过30000 由校长批
public abstract class Approver {
Approver approver;//下一个处理者
String name;
public Approver(String name) {
this.name = name;
}
public void setApprover(Approver approver) {
this.approver = approver;
}
//处理审批请求方法
public abstract void processRequest(PurchaseRequest purchaseRequest);
}
public class DepartmentApprover extends Approver{
public DepartmentApprover(String name) {
super(name);
}
@Override
public void processRequest(PurchaseRequest purchaseRequest) {
if(purchaseRequest.getPrice() <= 5000){
System.out.println("请求编号id =" + purchaseRequest.getId() + "被" + this.name + "处理");
}else {
approver.processRequest(purchaseRequest);
}
}
}
public class CollegeApprover extends Approver{
public CollegeApprover(String name) {
super(name);
}
@Override
public void processRequest(PurchaseRequest purchaseRequest) {
if(purchaseRequest.getPrice() > 5000 && purchaseRequest.getPrice() <= 10000){
System.out.println("请求编号id =" + purchaseRequest.getId() + "被" + this.name + "处理");
}else {
approver.processRequest(purchaseRequest);
}
}
}
public class ViceSchoolMasterApprover extends Approver{
public ViceSchoolMasterApprover(String name) {
super(name);
}
@Override
public void processRequest(PurchaseRequest purchaseRequest) {
if(purchaseRequest.getPrice() > 10000 && purchaseRequest.getPrice() <= 30000){
System.out.println("请求编号id =" + purchaseRequest.getId() + "被" + this.name + "处理");
}else {
approver.processRequest(purchaseRequest);
}
}
}
public class SchoolMasterApprover extends Approver{
public SchoolMasterApprover(String name) {
super(name);
}
@Override
public void processRequest(PurchaseRequest purchaseRequest) {
if( purchaseRequest.getPrice() > 30000){
System.out.println("请求编号id =" + purchaseRequest.getId() + "被" + this.name + "处理");
}else {
approver.processRequest(purchaseRequest);
}
}
}
public class PurchaseRequest {
private int type;
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;
}
}
public class Client {
public static void main(String[] args) {
//创建一个请求
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);
}
}
/**
请求编号id =1被周校长处理
**/
SpringMVC-HandlerExecutionChain类就使用到职责链模式
总结
1)将请求和处理分开,实现解耦,提高灵活性
2)简化了对象,使对象不需要知道链的结构
3)性能会受到影响,特别是在链比较长的时候,因此需要控制链中最大节点数量,一般通过Handler中设置一个最大节点数量,在setNext()方法中判断是否已经超过阈值,超过则不允许该链建立,避免出现超长链无意识的破坏系统性能
4)调试不方便,采用了类似递归的方式,调试时逻辑可能比较复杂