使用策略模式时的一个生效问题

策略模式的替换场景:

1:产品有默认策略A,B,项目扩展策略C,此为正常扩展。

2:产品有默认策略A,B,项目需要改写策略B,此为项目替换默认策略。

3:产品有默认策略A,B,项目扩展策略C,产品需要反向扩展策略C,并对C进行修改。此时,由于项目的策略是先生成的,处于已使用的状态,产品属于是后补充,不能对项目C策略有影响。此为产品反向补充策略C.

也不知说明白没有,其实也简单,两个布尔值随便玩下

java 复制代码
public interface ColumnCondition{
	// 是否是产品出厂自带的策略
    default boolean isOriginal() {
        return false;
    }
	// 是否是项目替换的策略
    default boolean isReplace() {
        return false;
    }
	// 支持的策略标识
    boolean support(String businessCode);
	// 处理上下文
    void doBusiness(Context context);

判断逻辑如下:

java 复制代码
	@AutowiredFalse
    private List<ColumnCondition> columnConditions; // 所有策略

   public ColumnCondition matchColumnCondition(String businessCode) {
   		// 没有策略
        if (ListUtils.isEmptyList(columnConditions)) {
            return null;
        }

		// 匹配出支持当前code的策略
        List<ColumnCondition> matchConditions = ListUtils.collectCondition(this.columnConditions, c -> c.support(businessCode));
        if (ListUtils.isEmptyList(matchConditions)) {
            return null;
        }
        // 情况1:只有一种策略,直接使用即可
        if (ListUtils.isSingletonList(matchConditions)) {
            return matchConditions.get(FIRST);
        }

        // 情况2:项目替换产口的某条策略
        for (ColumnCondition matchCondition : matchConditions) {
            if (matchCondition.isReplace()) {
                return matchCondition;
            }
        }

        // 情况3:产品改写项目已有的策略,不对项目的该策略产生影响
        for (ColumnCondition matchCondition : matchConditions) {
            if (!matchCondition.isOriginal()) {
                return matchCondition;
            }
        }
        // 返回第一条策略:此处是不精确的,但也是能执行的。一般业务是能满足的
        return matchConditions.get(FIRST);
    }
相关推荐
dyxal19 小时前
使用tree命令导出文件夹/文件的目录树( Windows 和 macOS)
windows·macos·策略模式
酷炫码神19 小时前
第 2 篇:Java 入门实战(JDK8 版)—— 编写第一个 Java 程序,理解基础运行逻辑
java·开发语言·策略模式
程序员TNT2 天前
Shoptnt 促销计算引擎详解:策略模式与责任链的完美融合
linux·windows·策略模式
new_daimond3 天前
设计模式-策略模式深度分析
设计模式·策略模式
织_网3 天前
Electron 核心模块速查表
javascript·electron·策略模式
至此流年莫相忘3 天前
设计模式:策略模式
设计模式·策略模式
特种加菲猫5 天前
并发编程的守护者:信号量与日志策略模式解析
linux·笔记·策略模式
xiaowu0805 天前
策略模式-不同的鸭子的案例
开发语言·c#·策略模式
彭于晏Yan6 天前
Spring Boot中策略模式结合依赖注入的实现方式
spring boot·策略模式
宁静致远20216 天前
【C++设计模式】第二篇:策略模式(Strategy)--从基本介绍,内部原理、应用场景、使用方法,常见问题和解决方案进行深度解析
c++·设计模式·策略模式