职责链模式
避免将一个请求的发送者与接收者耦合在一起,让多个对象都有机会处理请求。将接收请求的对象连接成一条链,并且沿着这条链传递请求,直到有一个对象能够处理它为止,是一种对象行为型模式
类图
实现
抽象处理者
package design.cor.realize;
/*
*
*@author:zzf
*@time:2020-12-20
*
*/
public abstract class Handler {
//维持对下家的引用
protected Handler successor;
public void setSuccessor(Handler successor){
this.successor=successor;
}
public abstract void handleRequest(String request);
}
具体处理者
package design.cor.realize;
/*
*
*@author:zzf
*@time:2020-12-20
*
*/
public class ConcreteHandlerA extends Handler {
@Override
public void handleRequest(String request) {
if(true){
//处理
}
else {
//转发请求
this.successor.handleRequest(request);
}
}
}
package design.cor.realize;
/*
*
*@author:zzf
*@time:2020-12-20
*
*/
public class ConcreteHandlerB extends Handler {
@Override
public void handleRequest(String request) {
if(true){
//处理
}
else {
//转发请求
this.successor.handleRequest(request);
}
}
}
客户端
package design.cor.realize;
/*
*
*@author:zzf
*@time:2020-12-20
*
*/
public class Client {
public static void main(String[] args) {
Handler handler1,handler2;
handler1=new ConcreteHandlerA();
handler2=new ConcreteHandlerB();
//创建职责链
handler1.setSuccessor(handler2);
//发送请求,请求对象通常为自定义类型
handler1.handleRequest("请求对象");
}
}
应用实例
某企业的SCM(Supply Chain Management,供应链管理)系统中包含一个采购审批子系统。该企业的采购审批是分级进行的,即根据采购金额的不同由不同层次的主管人员来审批,主任可以审批5万元以下(不包括5万元)的采购单,副董事长可以审批5万元至10万元(不包括10万元)的采购单,董事长可以审批10万元至50万元(不包括50万元)的采购单,50万元及以上的采购单就需要开董事会讨论决定。如下图所示:
现使用职责链模式设计并实现该系统,类图如下:
代码实现
采购单类,充当请求类
package design.cor;
public class PurchaseRequest {
private double amount;// 采购金额
private int number;// 采购单编号
private String purpose;// 采购目的
public PurchaseRequest(double amount, int number, String purpose) {
this.amount = amount;
this.number = number;
this.purpose = purpose;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getPurpose() {
return purpose;
}
public void setPurpose(String purpose) {
this.purpose = purpose;
}
}
抽象处理者
package design.cor;
public abstract class Approver {
protected Approver successor;// 后继对象
protected String name;
public Approver(String name) {
this.name = name;
}
// 设置后继者
public void setSuccessor(Approver successor) {
this.successor = successor;
}
// 抽象请求处理方法
public abstract void processRequest(PurchaseRequest request);
}
具体处理者
package design.cor;
public class Director extends Approver {
public Director(String name) {
super(name);
}
@Override
public void processRequest(PurchaseRequest request) {
if (request.getAmount() < 50000) {
System.out.println("主任" + this.name + "审批采购单:" + request.getNumber() + "," + "金额:" + request.getAmount()
+ "元,采购目的:" + request.getPurpose() + ".");
} else {
this.successor.processRequest(request);// 转发请求
}
}
}
package design.cor;
public class VicePresident extends Approver {
public VicePresident(String name) {
super(name);
}
@Override
public void processRequest(PurchaseRequest request) {
if (request.getAmount() < 100000) {
System.out.println("副董事长" + this.name + "审批采购单:" + request.getNumber() + "," + "金额:" + request.getAmount()
+ "元,采购目的:" + request.getPurpose() + ".");
} else {
this.successor.processRequest(request);// 转发请求
}
}
}
package design.cor;
public class President extends Approver {
public President(String name) {
super(name);
}
@Override
public void processRequest(PurchaseRequest request) {
if (request.getAmount() < 500000) {
System.out.println("董事长" + this.name + "审批采购单:" + request.getNumber() + "," + "金额:" + request.getAmount()
+ "元,采购目的:" + request.getPurpose() + ".");
} else {
this.successor.processRequest(request);// 转发请求
}
}
}
package design.cor;
public class Congress extends Approver {
public Congress(String name) {
super(name);
}
@Override
public void processRequest(PurchaseRequest request) {
System.out.println("召开董事会审批采购单:" + request.getNumber() + "," + "金额:" + request.getAmount() + "元,采购目的:"
+ request.getPurpose() + ".");
}
}
客户端
package design.cor;
public class Client {
public static void main(String[] args) {
Approver wjzhang, rhuang, gyang, jguo, meeting;
wjzhang = new Director("张无忌");
rhuang = new Manager("黄蓉");
gyang = new VicePresident("杨过");
jguo = new President("郭靖");
meeting = new Congress("董事会");
// 创建职责链
wjzhang.setSuccessor(rhuang);
rhuang.setSuccessor(gyang);
rhuang.setSuccessor(jguo);
jguo.setSuccessor(meeting);
// 创建采购单
PurchaseRequest pr1 = new PurchaseRequest(45000, 10001, "购买倚天剑");
wjzhang.processRequest(pr1);
PurchaseRequest pr2 = new PurchaseRequest(60000, 10002, "购买《葵花宝典》");
wjzhang.processRequest(pr2);
PurchaseRequest pr3 = new PurchaseRequest(160000, 10003, "购买《金刚经》");
wjzhang.processRequest(pr3);
PurchaseRequest pr4 = new PurchaseRequest(800000, 10004, "购买桃花岛");
wjzhang.processRequest(pr4);
}
}
上述项目中进行扩展,增加一个新的具体处理者,角色为经理,负责审批5万元-8万元(不包括8万元)的采购单。
增加具体处理类
package design.cor;
public class Manager extends Approver {
public Manager(String name) {
super(name);
}
@Override
public void processRequest(PurchaseRequest request) {
if (request.getAmount() < 80000) {
System.out.println("经理" + this.name + "审批采购单:" + request.getNumber() + "," + "金额:" + request.getAmount()
+ "元,采购目的:" + request.getPurpose() + ".");
} else {
this.successor.processRequest(request);// 转发请求
}
}
}
修改客户端
package design.cor;
public class Client {
public static void main(String[] args) {
Approver wjzhang, rhuang, gyang, jguo, meeting;
wjzhang = new Director("张无忌");
rhuang = new Manager("黄蓉");
gyang = new VicePresident("杨过");
jguo = new President("郭靖");
meeting = new Congress("董事会");
// 创建职责链
wjzhang.setSuccessor(rhuang);
rhuang.setSuccessor(gyang);
rhuang.setSuccessor(jguo);
jguo.setSuccessor(meeting);
// 创建采购单
PurchaseRequest pr1 = new PurchaseRequest(45000, 10001, "购买倚天剑");
wjzhang.processRequest(pr1);
PurchaseRequest pr2 = new PurchaseRequest(60000, 10002, "购买《葵花宝典》");
wjzhang.processRequest(pr2);
PurchaseRequest pr3 = new PurchaseRequest(160000, 10003, "购买《金刚经》");
wjzhang.processRequest(pr3);
PurchaseRequest pr4 = new PurchaseRequest(800000, 10004, "购买桃花岛");
wjzhang.processRequest(pr4);
}
}
运行结果
纯的职责链模式
一个纯的职责链模式要求一个具体处理者对象只能在两个行为中选择一个,要么承担全部责任,要么将责任推给大家,不允许出现某一个具体处理者对象在承担了一部分或全部责任后又将责任向下传递的情况。而且在纯的职责链模式中要求一个请求必须被某一个处理者对象所接收,不能出现某个请求未被任何一个处理者对象处理的情况
不纯的职责链模式
在一个不纯的职责链模式中允许某个请求被一个具体处理者部分处理后再向下传递,或者一个具体处理者处理完某请求后其后继处理者可以继续处理该请求,而且一个请求可以最终不被任何处理者对象所接收并处理
职责链模式的优缺点
优点
(1)是一个对象无须知道是其他哪一个对象处理其请求,降低了系统的耦合度。
(2)简化了对象之间的相互连接
(3)给对象职责的分配带来更多的灵活性
(4)增加一个新的具体请求处理者较为方便
缺点
(1)不能保证请求一定会被处理
(2)对于比较长的职责链,系统性能将受到一定的影响
(3)如果建链不当,可能会造成循环调用,将导致系统陷入死循环
适用环境
(1)有多个对象可以处理同一请求,具体哪个对象处理该请求待运行时刻再确定
(2)在不明确指定接收者的情况下,向多个对象中的一个提交一个请求
(3)可动态指定一组对象处理请求,客户端可以动态创建职责链来处理请求,还可以改变链中处理者之间的先后次序
参考
Java设计模式