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

策略模式的替换场景:

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);
    }
相关推荐
T0uken1 分钟前
【Mac】WireGuard:使用 launchd 管理服务
macos·策略模式
橘色的喵1 天前
嵌入式C语言编程:策略模式、状态模式和状态机的应用
c语言·状态模式·策略模式·状态机
Code blocks3 天前
SpringBoot中策略模式使用
java·spring boot·后端·mybatis·策略模式
Yang-Never4 天前
设计模式 -> 策略模式(Strategy Pattern)
android·开发语言·设计模式·kotlin·android studio·策略模式
pointers_syc5 天前
【设计模式】2.策略模式
java·设计模式·策略模式
oioihoii5 天前
在macOS上使用VS Code和Clang配置C++开发环境
c++·macos·策略模式
ghostwritten6 天前
macOS安装配置Unbound DNS完整指南
macos·策略模式·dns
超龄超能程序猿6 天前
Vue3 + Electron 技术栈下 MAC 地址获取的方法、准确性优化与应对策略
macos·electron·策略模式
万粉变现经纪人7 天前
如何解决pip安装报错ModuleNotFoundError: No module named ‘dash’问题
python·scrapy·pycharm·flask·pip·策略模式·dash
IT小白架构师之路8 天前
常用设计模式系列(十六)—策略模式
设计模式·bash·策略模式