责任链模式

使用场景:多个连续的功能上不相互依赖的处理环节,共同完成一项任务。

特点:(1)顺序执行

(2) 有多个处理环节

比较:与观察者模式不同的是,观察者模式中所有的订阅者可并行执行,而责任链模式具有顺序执行的特点;

其次观察者模式中所有的订阅者没有任何关联,而责任链模式中的每个处理环节具有传递性。

模式举例:

(1)公共接口

复制代码
public interface StrategyInterface {
    void execute(Object obj);
}

(2)抽象实现类

复制代码
@Data
public abstract class AbstractStrategy implements StrategyInterface{
​
    protected StrategyInterface  strategyInterface;

    public void next(Object obj){
        if (strategyInterface != null){
            strategyInterface.execute(obj);
        }
    }
}
​

(3)实现类-第一环节

复制代码
@Slf4j
@Component
public class StrategyHandle extends AbstractStrategy {
    @Override
    public void execute(Object obj) {
        try {
            log.info("first StrategyHandle task begin..... ");
            next(obj);
            successHandle(obj);
        }catch (Exception e){
            errorHandle(obj);
        }
    }
​
    private void successHandle(Object obj) {
        log.info("first task success over");
    }
​
    private void errorHandle(Object obj) {
        log.error("first task error");
​
    }
​
​
}

(4)实现类-第二环节

复制代码
@Slf4j
@Component
public class StrategyCheck extends AbstractStrategy {
    @Override
    public void execute(Object obj) {
        log.info("second StrategyCheck task begin......");
        checkBefore(obj);
        next(obj);
        checkAfter(obj);
    }
​
    private void checkAfter(Object obj) {
        log.info("second StrategyCheck task 后置校验");
    }
​
    private void checkBefore(Object obj) {
        log.info("second StrategyCheck task 前置校验");
    }
}

(5)实现类-第三环节

复制代码
@Slf4j
@Component
public class StrategyCalculate extends AbstractStrategy {
    @Override
    public void execute(Object obj) {
        log.info("third StrategyCalculate task begin......");
        calculate(obj);
        next(obj);
    }
    public void calculate(Object obj){
        log.info("third 执行任务。。。");
    }
}
​

(6)组装链式调用

复制代码
@Service
public class StrategyService {
​
    @Autowired
    StrategyHandle strategyHandle;
​
    @Autowired
    StrategyCheck strategyCheck;
​
    @Autowired
    StrategyCalculate strategyCalculate;
​
    public void execute(Object obj){
        strategyHandle.setStrategyInterface(strategyCheck);
        strategyCheck.setStrategyInterface(strategyCalculate);
        strategyCalculate.setStrategyInterface(null);
        strategyHandle.execute(obj);
    }
}

(7)测试

复制代码
@SpringBootTest
@Slf4j
class Demo17ApplicationTests {
​
    @Autowired
    StrategyService strategyService;
​
    @Test
    void contextLoads() {
        strategyService.execute(new Object());
    }
}
相关推荐
怪兽201410 分钟前
fastjson在kotlin不使用kotlin-reflect库怎么使用?
android·开发语言·kotlin
ClearLiang10 分钟前
Kotlin-协程的挂起与恢复
开发语言·kotlin
彭同学学习日志15 分钟前
Kotlin Fragment 按钮跳转报错解决:Unresolved reference ‘floatingActionButton‘
android·开发语言·kotlin
海域云赵从友20 分钟前
破解跨境数据传输瓶颈:中国德国高速跨境组网专线与本地化 IP 的协同策略
开发语言·php
咚咚王者26 分钟前
人工智能之编程进阶 Python高级:第九章 爬虫类模块
开发语言·python
会编程的林俊杰26 分钟前
SpringBoot项目启动时的依赖处理
java·spring boot·后端
一叶飘零_sweeeet39 分钟前
深度拆解汽车制造系统设计:用 Java + 设计模式打造高扩展性品牌 - 车型动态生成架构
java·设计模式·工厂设计模式
深蓝海拓1 小时前
使matplot显示支持中文和负号
开发语言·python
王家羽翼-王羽1 小时前
nacos 3.1.0 运行主类报错 com.alibaba.cloud.nacos.logging.NacosLoggingAppRunListener
java
syt_biancheng2 小时前
Day3算法训练(简写单词,dd爱框框,3-除2!)
开发语言·c++·算法·贪心算法