【設(shè)計(jì)模式】通過飛書的審批流了解責(zé)任鏈模式
背景
審批一個(gè)內(nèi)容,可以先從基層管理者(Handler A)開始,如果經(jīng)過基層管理者無法滿足審批條件(handle),將到高層管理者(Handler B)進(jìn)行審批。
每個(gè)人審批節(jié)點(diǎn)只處理自己能力范圍內(nèi)的事情,這就和責(zé)任鏈模式十分吻合了。
模式定義
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.
通過給多個(gè)對(duì)象處理請(qǐng)求的機(jī)會(huì),避免將請(qǐng)求的發(fā)送方與其接收方耦合在一起。鏈接接收對(duì)象并沿著鏈傳遞請(qǐng)求,直到一個(gè)對(duì)象處理它。
模式結(jié)構(gòu)
模式實(shí)現(xiàn)
1.節(jié)點(diǎn)
審批節(jié)點(diǎn)定義
package com.example.designpattern.chainofresponsibility.handler;
/**
* 責(zé)任鏈節(jié)點(diǎn)
*
* @author hongcunlin
*/
public abstract class Handler {
/**
* 下一個(gè)審批節(jié)點(diǎn)
*/
protected Handler next;
/**
* 處理
*
* @param amount 金額
*/
public abstract void handle(int amount);
/**
* 設(shè)置下一個(gè)節(jié)點(diǎn)
*
* @param next 節(jié)點(diǎn)
*/
public void setNext(Handler next) {
this.next = next;
}
}
審批節(jié)點(diǎn)實(shí)現(xiàn),分別是組長、經(jīng)理、總監(jiān)
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;
/**
* 經(jīng)理
*
* @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;
/**
* 總監(jiān)
*
* @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.責(zé)任鏈
構(gòu)建團(tuán)組長、經(jīng)理、總監(jiān)的審批順序金額上限由低到高
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;
/**
* 責(zé)任鏈
*
* @author hongcunlin
*/
@Component("handlerChain")
public class HandlerChain {
/**
* 組長
*/
@Resource(name = "teamLeader")
private Handler teamLeader;
/**
* 經(jīng)理
*/
@Resource(name = "manager")
private Handler manager;
/**
* 總監(jiān)
*/
@Resource(name = "director")
private Handler director;
/**
* 構(gòu)建責(zé)任鏈
*/
@PostConstruct
public void init() {
teamLeader.setNext(manager);
manager.setNext(director);
}
/**
* 處理請(qǐng)求
*
* @param amount 金額
*/
public void handleRequest(int amount) {
teamLeader.handle(amount);
}
}
3.測(cè)試
package com.example.designpattern.chainofresponsibility;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
/**
* 責(zé)任鏈模式測(cè)試
*
* @author hongcunlin
*/
@SpringBootTest
public class DesignPatternTest {
/**
* 責(zé)任鏈
*/
@Resource(name = "handlerChain")
private HandlerChain handlerChain;
/**
* 測(cè)試審批
*/
@Test
public void test() {
handlerChain.handleRequest(750);
}
}
可以看到750元費(fèi)用的審批,是輪到經(jīng)理審批的,沒問題
500<750<1000
回顧
本文對(duì)飛書審批流節(jié)點(diǎn)的審批,采用責(zé)任鏈模式實(shí)現(xiàn),同時(shí)是基于項(xiàng)目開發(fā)中必用的Spring框架的,貼近實(shí)際開發(fā)。
有空再通過日常生活,聊聊其中涉及的設(shè)計(jì)模式。