责任链模式(代码可直接使用)
定义:为了避免请求发送者与多个请求处理者耦合在一起,将所有请求的处理者通过前一对象记住其下一个对象的引用而连成一条链;当有请求发生时,可将请求沿着这条链传递,直到有对象处理它为止
简单理解:当一个请求可能需要多人共同完成,A无法完成时,需要推给B,B处理不了需要推给C这种情况下。A、B、C形成一个链条,沿着这个链条,这个请求就能被处理。
模拟场景:一个公司报销流程如下,采购员在采购时,如果采购金额在5W以内,部门主管可以直接审批;
如果在10W以内,副董事长可以直接审批;如果在20W以内,董事长可以直接审批;如果高于20W则需要召开董事会商量。
//抽象处理器
public abstract class Handler<T extends Request> {
protected Handler nextHandler;
public void setNextHandler(Handler nextHandler){
this.nextHandler = nextHandler;
}
public abstract void handlerRequest(T t);
}
//抽象的请求
@Data
public abstract class Request {
public String id;
}
//采购请求
@Data
@AllArgsConstructor
public class PurchaseResquest extends Request {
private double amount; //购买金额
private String purpose; //购买目的
}
//对于购买请求的统一处理器
public abstract class ApproverHandler<T extends PurchaseResquest> extends Handler<T> {
protected String name;
public ApproverHandler(String name){
this.name = name;
}
@Override
public void setNextHandler(Handler nextHandler) {
super.setNextHandler(nextHandler);
}
@Override
public abstract void handlerRequest(T request);
}
//部门主管
public class DirectorHandler<T extends PurchaseResquest> extends ApproverHandler<T> {
public DirectorHandler(String name) {
super(name);
}
@Override
public void handlerRequest(PurchaseResquest request) {
if (request.getAmount()<5) {
System.out.println(String.format("部门主管%s审批了该购买请求:金额为:%s 购买目的为:%s",name,request.getAmount(),request.getPurpose()));
}else{
System.out.println(String.format("部门主管无权审批该申请"));
this.nextHandler.handlerRequest(request);
}
}
}
//副董事长
public class Vicepresident<T extends PurchaseResquest> extends ApproverHandler<T> {
public Vicepresident(String name) {
super(name);
}
@Override
public void handlerRequest(PurchaseResquest request) {
if (request.getAmount()>5 && request.getAmount() < 10) {
System.out.println(String.format("副董事长%s审批了该购买请求:金额为:%s 购买目的为:%s",name,request.getAmount(),request.getPurpose()));
}else{
nextHandler.handlerRequest(request);
}
}
}
/董事长
public class PresidentHandler<T extends PurchaseResquest> extends ApproverHandler<T> {
public PresidentHandler(String name) {
super(name);
}
@Override
public void handlerRequest(PurchaseResquest request) {
if (request.getAmount()>10 && request.getAmount() < 20) {
System.out.println(String.format("董事长%s审批了该购买请求,购买金额为%s,购买目的为%s",super.name,request.getAmount(),request.getPurpose()));
}else{
nextHandler.handlerRequest(request);
}
}
}
//董事会
public class CongressHandler<T extends PurchaseResquest> extends ApproverHandler<T> {
public CongressHandler(String name) {
super(name);
}
@Override
public void handlerRequest(PurchaseResquest request) {
System.out.println(String.format("董事会审批了该购买请求:金额为:%s 购买目的为:%s",request.getAmount(),request.getPurpose()));
}
}
//测试类
public class Test {
public static void main(String[] args) {
double amount = 22;
PurchaseResquest purchaseResquest = new PurchaseResquest(amount,"买办公用品");
Handler directorHandler = new DirectorHandler("张三");
Handler vicepresident = new Vicepresident("李四");
Handler presidentHandler = new PresidentHandler("王五");
Handler congressHandler = new CongressHandler("赵六");
directorHandler.setNextHandler(vicepresident);
vicepresident.setNextHandler(presidentHandler);
presidentHandler.setNextHandler(congressHandler);
directorHandler.handlerRequest(purchaseResquest);
}
}
//测试结果
部门主管无权审批该申请
董事会审批了该购买请求:金额为:22.0 购买目的为:买办公用品
Process finished with exit code 0
试着修改amount的值,处理者也是不一样的