前置内容
ps:【文章由来】 公司项目中所用的合同签章处理流程,本人基于责任链上使用策略模式进行优化。
签章的处理流程
执行的流程如下:
整个结构类似于递归调用。每个节点中依赖上一个节点的输入以及下一个节点的输出,在中间过程可以实现每个节点的自定义操作,比较灵活。
流程实现
GitHub地址
项目结构
DesignPatterns
└── src
└── main
└── java
└── com.xbhog.chainresponsibility
├── annotations
│ └── ContractSign
├── channel
│ ├── ContractSignChannelImpl.java
│ └── ContractSignChannel
├── Config
│ └── SignConfig
├── Enum
│ └── ContractSignEnum
├── impl
│ ├── ContractSignCompactInitImpl.java
│ ├── ContractSignGenerateImpl.java
│ ├── ContractSignMockImpl.java
│ ├── ContractSignMqImpl.java
│ ├── ContractSignSaveUploadImpl.java
│ ├── ContractSignSerialImpl.java
│ └── ContractSignTradeImpl.java
├── inter
│ ├── Call
│ ├── Chain
│ ├── Interceptor
│ └── Processor
├── pojo
│ ├── ContractRequest.java
│ └── ContractResponse.java
├── ContractCall
├── ContractChain
└── ContractSignProcessor.java
项目类图
责任链+组合模式代码实现
工程结构
DesignPatterns
└── src
└── main
└── java
└── com.xbhog.chainresponsibility
├── channel
│ ├── ContractSignChannelImpl.java
│ └── ContractSignChannel
├── impl
│ ├── ContractSignCompactInitImpl.java
│ ├── ContractSignGenerateImpl.java
│ ├── ContractSignMockImpl.java
│ ├── ContractSignMqImpl.java
│ ├── ContractSignSaveUploadImpl.java
│ ├── ContractSignSerialImpl.java
│ └── ContractSignTradeImpl.java
├── inter
│ ├── Call
│ ├── Chain
│ ├── Interceptor
│ └── Processor
├── pojo
│ ├── ContractRequest.java
│ └── ContractResponse.java
├── ContractCall
├── ContractChain
└── ContractSignProcessor.java
责任链中的对象定义
//请求
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class ContractRequest {
private String name;
private String age;
private String status;
}
//响应
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class ContractResponse {
private String status;
private String mas;
}
定义流程中的请求及响应类,方便处理每个责任链的请求、返回信息。
责任链处理流程
/**
* @author xbhog
* @describe: 责任链+组合实现合同签章
* @date 2023/7/11
*/
@Slf4j
@Component
public class ContractSignProcessor implements Processor {
@Resource(name = "contractSignCompactInitImpl")
private Interceptor contractCompactInitImpl;
......
public ContractSignProcessor() {
}
@Override
public ContractResponse process(T paramter) {
//获取所有的监听器
List interceptorList = new ArrayList();
interceptorList.add(contractCompactInitImpl);
......
//开始签章
log.info("签章开始");
return new ContractCall(paramter,interceptorList).exectue();
}
}
合同签章方法的主流程调用接口(入口) ,该类中注入所有的节点实现类(如contractCompactInitImpl
),通过编排实现责任链流程。 在初始化节点之前,进行节点的封装以及数据请求的处理。例:contractCompactInitImpl
-合同数据初始化节点
/**
* @author xbhog
* @describe: 合同数据请求、节点的实例化及方法执行
* @date 2023/7/11
*/
public class ContractCall implements Call {
private final T originalRequest;
private final List interceptorList;
public ContractCall(T originalRequest, List interceptorList) {
this.originalRequest = originalRequest;
this.interceptorList = interceptorList;
}
@Override
public T request() {
return this.originalRequest;
}
@Override
public ContractResponse exectue() {
//实例化流程节点
ContractChain chain = new ContractChain(0,this.originalRequest,this.interceptorList);
return chain.proceed(this.originalRequest);
}
}
获取节点中的请求参数,实例化当前责任链节点(contractCompactInitImpl
),在执行节点中的proceed
方法来获取当前节点的参数以及获取节点的信息。
/**
* @author xbhog
* @describe: 合同节点
* @date 2023/7/11
*/
@Slf4j
public class ContractChain implements Chain {
private final Integer index;
private final T request;
private final List interceptors;
public ContractChain(Integer index, T request, List interceptors) {
this.index = index;
this.request = request;
this.interceptors = interceptors;
}
@Override
public T request() {
return this.request;
}
@Override
public ContractResponse proceed(T request) {
//控制节点流程
if(this.index >= this.interceptors.size()){
throw new IllegalArgumentException("index越界");
}
//下一个节点参数设置
Chain nextChain = new ContractChain(this.index + 1, request, this.interceptors);
//获取节点信息
Interceptor interceptor = this.interceptors.get(this.index);
Class