每个人审批节点只处理自己能力范围内的事情,这就和责任链模式十分吻合了。
模式定义
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;
public abstract class Handler {
protected Handler next;
public abstract void handle(int amount);
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;
@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;
@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;
@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;
@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);
}
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;
@SpringBootTest
public class DesignPatternTest {
@Resource(name = "handlerChain")
private HandlerChain handlerChain;
@Test
public void test() {
handlerChain.handleRequest(750);
}
}
可以看到750元费用的审批,是轮到经理审批的,没问题
500<750<1000
回顾
本文对飞书审批流节点的审批,采用责任链模式实现,同时是基于项目开发中必用的Spring框架的,贴近实际开发。
有空再通过日常生活,聊聊其中涉及的设计模式。