背景
审批一个内容,可以先从基层管理者(Handler A)开始,如果经过基层管理者无法满足审批条件(handle),将到高层管理者(Handler B)进行审批。
每个人审批节点只处理自己能力范围内的事情,这就和责任链模式十分吻合了。
模式定义
Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.
通过给多个对象处理请求的机会,避免将请求的发送方与其接收方耦合在一起。链接接收对象并沿着链传递请求,直到一个对象处理它。
模式结构
模式实现
1.节点
审批节点定义
package com.example.designpattern.chainofresponsibility.handler;
/**
* 责任链节点
*
* @author hongcunlin
*/
public abstract class Handler {
/**
* 下一个审批节点
*/
protected Handler next;
/**
* 处理
*
* @param amount 金额
*/
public abstract void handle(int amount);
/**
* 设置下一个节点
*
* @param next 节点
*/
public void setNext(Handler next) {
this.next = next;
}
}
审批节点实现,分别是组长、经理、总监
package com.example.designpattern.chainofresponsibility.handler.impl;
import com.example.designpattern.chainofresponsibility.handler.Handler;
import org.springframework.stereotype.Component;
/**
* 组长
*
* @author hongcunlin
*/
@Component("teamLeader")
public class TeamLeader extends Handler {
/**
* 上限金额
*/
private static final Integer LIMITED_AMOUNT = 500;
@Override
public void handle(int amount) {
if (amount < LIMITED_AMOUNT) {
System.out.println("TeamLeader approved");
} else if (null != next) {
next.handle(amount);
}
}
}
package com.example.designpattern.chainofresponsibility.handler.impl;
import com.example.designpattern.chainofresponsibility.handler.Handler;
import org.springframework.stereotype.Component;
/**
* 经理
*
* @author hongcunlin
*/
@Component("manager")
public class Manager extends Handler {
/**
* 上限金额
*/
private static final Integer LIMITED_AMOUNT = 1000;
@Override
public void handle(int amount) {
if (amount < LIMITED_AMOUNT) {
System.out.println("Manager approved");
} else if (null != next) {
next.handle(amount);
}
}
}
package com.example.designpattern.chainofresponsibility.handler.impl;
import com.example.designpattern.chainofresponsibility.handler.Handler;
import org.springframework.stereotype.Component;
/**
* 总监
*
* @author hongcunlin
*/
@Component("director")
public class Director extends Handler {
/**
* 上限金额
*/
private static final Integer LIMITED_AMOUNT = 1000;
@Override
public void handle(int amount) {
if (amount < LIMITED_AMOUNT) {
System.out.println("Director approved");
} else if (null != next) {
next.handle(amount);
}
}
}
2.责任链
构建团组长、经理、总监的审批顺序金额上限由低到高
package com.example.designpattern.chainofresponsibility;
import com.example.designpattern.chainofresponsibility.handler.Handler;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
/**
* 责任链
*
* @author hongcunlin
*/
@Component("handlerChain")
public class HandlerChain {
/**
* 组长
*/
@Resource(name = "teamLeader")
private Handler teamLeader;
/**
* 经理
*/
@Resource(name = "manager")
private Handler manager;
/**
* 总监
*/
@Resource(name = "director")
private Handler director;
/**
* 构建责任链
*/
@PostConstruct
public void init() {
teamLeader.setNext(manager);
manager.setNext(director);
}
/**
* 处理请求
*
* @param amount 金额
*/
public void handleRequest(int amount) {
teamLeader.handle(amount);
}
}
3.测试
package com.example.designpattern.chainofresponsibility;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
/**
* 责任链模式测试
*
* @author hongcunlin
*/
@SpringBootTest
public class DesignPatternTest {
/**
* 责任链
*/
@Resource(name = "handlerChain")
private HandlerChain handlerChain;
/**
* 测试审批
*/
@Test
public void test() {
handlerChain.handleRequest(750);
}
}
可以看到750元费用的审批,是轮到经理审批的,没问题
500