从if-else到策略注册表的演进

2025-06-20  本文已影响0人  了不起的ACER

概述

本文档基项目中的StrategyRegistry实现,深入分析从传统if-else条件判断到现代策略注册表模式的演进过程。

演进历程

第一阶段:简单if-else(史前时代)

最原始的条件分发方式,适用于简单场景:

public class Service {
    public ResponseDto exec(String type, Object data) {
        if ("1".equals(type)) {
            return handle1();
        } else if ("2".equals(type)) {
            return handle2();
        } else {
            throw new ServiceException("不支持的认证类型: " + type);
        }
    }
}

特点:

问题:

第二阶段:复杂if-else链(古代)

随着业务复杂度增加,条件判断变得复杂:

public class Service {
    public ResponseDto exec(ActionType actionType, Command command) {
        if (ActionType.A.equals(actionType)) {
            if (command.getLoginDto().getType() == Type.1) {
                return handle1(command.getDto());
            } else if (command.getDto().getType() == Type.2) {
                return handle2(command.getDto());
            }
        } else if (ActionType.B.equals(actionType)) {
            if (command.getDto().getType() == Type.3) {
                return handle3(command.getDto());
            } else if (command.getDto().getype() == Type.4) {
                return handle4(command.getDto());
            }
        }
        throw new ServiceException("不支持的认证类型: " + actionType);
    }
}

问题加剧:

第三阶段:switch优化(近代)

使用switch语句优化可读性:

public class Service {
    public ResponseDto exec(ActionType actionType, Command command) {
        return switch (actionType) {
            case A -> handle1(command);
            case B -> handle2r(command);
            default -> throw new ServiceException("不支持的认证类型: " + actionType);
        };
    }
}

改进:

仍存在问题:

第四阶段:简单策略模式(现代早期)

引入策略模式,实现基本的策略分发:

public class hService {
    private final Map<ActionType, Strategy> strategies;

    public Service() {
        strategies = Map.of(
                ActionType.A, new Strategy(),
                ActionType.B, new Strategy()
        );
    }

    public ResponseDto henticate(ActionType actionType, Command command) {
        Strategy strategy = strategies.get(actionType);
        if (strategy == null) {
            throw new ServiceException("不支持的认证类型: " + actionType);
        }
        return strategy.execute(command);
    }
}

优势:

局限:

第五阶段:策略注册表(现代)

基于Spring容器的自动化策略注册表:

@Component
public class StrategyRegistry {

    private final Map<ActionType, Strategy> strategyMap = new HashMap<>();
    private Map<ActionType, Strategy> immutableStrategyMap;

    @owired(required = false)
    private List<Strategy> strategies = new ArrayList<>();

    @PostConstruct
    public void initRegistry() {
        // 按优先级排序
        strategies.sort(Comparator.comparingInt(Strategy::getPriority));

        // 自动注册所有策略实现
        for (Strategy strategy : strategies) {
            registerStrategy(strategy);
        }

        // 创建不可变视图
        immutableStrategyMap = Map.copyOf(strategyMap);
    }

    public Strategy getStrategy(ActionType actionType) {
        Strategy strategy = immutableStrategyMap.get(actionType);
        if (strategy == null) {
            throw new ServiceException("未找到认证类型 " + actionType + " 对应的策略");
        }
        return strategy;
    }
}

现代化特性:

推荐资源

-end-

上一篇 下一篇

猜你喜欢

热点阅读