职责链模式又叫命令链、CoR、Chain of Command、Chain of Responsibility。
该模式允许你将请求沿着处理者链进行发送,使多个对象都可以处理请求,每个对象有权决定处理或传递给下个节点。
客户端:用来定义职责链条。
处理者:声明了所有具体处理者的通用接口。 该接口通常仅包含单个方法用于请求处理, 但有时其还会包含一个设置链上下个处理者的方法。
具体处理类:处理请求的实际代码,决定是否处理以及向下传递。
接下来阐述下,职责链模式的各级构成,既然是响应请求,首先得有请求。这里以一个采购审批为例子。
请求者类(普通类):包含两个属性(编号:id 和价格:price)
public class Request {
//请求编号
private int id;
//请求价格
private float price;
/**
* @param id
* @param price
*/
public Request(int id, float price) {
super();
this.id = id;
this.price = price;
}
public int getId() {
return id;
}
public float getPrice() {
return price;
}
}
责任链的抽象处理类,顶层接口只生命,不做具体实现。
/**
* 类描述:责任链之抽象处理类
*
* @ClassName Handler
* @Author ward
* @Date 2023-09-01 18:26
*/
public abstract class Handler {
//创建实现对象,主要用于被定义
protected Handler successor;
//处理者的名称
String name;
/**
* @param name
* 构造器,传入名称即可
*/
public Handler(String name) {
super();
this.name = name;
}
public void setSuccessor(Handler successor) {
this.successor = successor;
}
//业务处理方法,抽象的
public abstract void process(ProcureRequest procureRequest);
}
主任处理类
/**
* 类描述:主任处理类
*
* @ClassName DirectorHandler
* @Author ward
* @Date 2023-09-02 10:54
*/
public class DirectorHandler extends Handler {
private static final float PRICE = 10000f;
/**
* @param name 构造器,传入名称即可
*/
public DirectorHandler(String name) {
super(name);
}
@Override
public void process(ProcureRequest procureRequest) {
//如果金额小于等于10000,由学校主任审批
if (procureRequest.getPrice() <= PRICE) {
System.out.println("请求编号: " + procureRequest.getId() + "被" + name + "处理");
} else {
System.out.println("请求编号: " + procureRequest.getId() + "," + name + "已签字");
successor.process(procureRequest);
}
}
}
财务处理类
/**
* 类描述:财务处理类
*
* @ClassName FinanceHandler
* @Author ward
* @Date 2023-09-02 10:38
*/
public class FinanceHandler extends Handler {
private static final float PRICE_1 = 10000f;
private static final float PRICE_2 = 20000f;
private static final String name = "财务";
public FinanceHandler(String name) {
super(name);
}
@Override
public void process(ProcureRequest procureRequest) {
//如果金额大于10000,小于20000,由学校财务审批
if (procureRequest.getPrice() > PRICE_1 && procureRequest.getPrice() <= PRICE_2) {
System.out.println("请求编号: " + procureRequest.getId() + "被" + name + "处理");
} else {
System.out.println("请求编号: " + procureRequest.getId() + "," + name + "已签字");
successor.process(procureRequest);
}
}
}
校长处理类
/**
* 类描述:校长处理类
*
* @ClassName SchoolMasterHandler
* @Author ward
* @Date 2023-09-02 10:56
*/
public class SchoolMasterHandler extends Handler {
private static final float PRICE = 20000f;
private static final String name = "校长";
/**
* @param name 构造器,传入名称即可
*/
public SchoolMasterHandler(String name) {
super(name);
}
@Override
public void process(ProcureRequest procureRequest) {
//如果金额大于20000,由学校校长审批
if (procureRequest.getPrice() > PRICE) {
System.out.println("请求编号: " + procureRequest.getId() + "被" + name + "处理");
} else {
System.out.println("请求编号: " + procureRequest.getId() + "," + name + "已签字");
successor.process(procureRequest);
}
}
}
这里就是关于创建完实际处理类之后我们如何去把链条建立起来,其实这个一般是封装起来的,然后。
/**
* 类描述:客户端
*
* @ClassName Client
* @Author ward
* @Date 2023-09-02 11:06
*/
public class Client {
public static void testChain(int id, int price) {
//创建请求对象
ProcureRequest procureRequest = new ProcureRequest(id, price);
//创建各个处理者
Handler directorHandler = new DirectorHandler("学校主任");
Handler financeHandler = new FinanceHandler("学校财务");
Handler schoolMasterHandler = new SchoolMasterHandler("学校校长");
//设置链条下一级
directorHandler.setSuccessor(financeHandler);
financeHandler.setSuccessor(schoolMasterHandler);
//开始处理
directorHandler.process(procureRequest);
}
public static void main(String[] args) {
int[] priceArr = {500, 10001, 20001};
int id = 1;
for (int price : priceArr
) {
testChain(id, price);
id++;
}
}
}
正文
在这里插入代码片