Java 策略模式(二)-实战

现在有个功能是根据工单的不同类型做对应的业务逻辑处理。

用策略模式来做

一、抽象出一个策略类

第一个方法是业务逻辑处理

第二个方法是返回工单类型

java 复制代码
public interface ProblemStrategy {

    void handle(CrmProblemNoticeFollow follow, String logMsg);

    Integer getSmallProblemId();
}

二、定义具体的策略实现类,下面贴出一个详细代码

java 复制代码
/**
 * 催签收
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class CQSProblemStrategy implements ProblemStrategy{
    private final WdgjImMessageMsUtil wdgjImMessageMsUtil;
    private final TrackUtil trackUtil;

    @Override
    public Integer getSmallProblemId() {
        return 606;
    }

    @Override
    public void handle(CrmProblemNoticeFollow follow, String logMsg) {
        log.info(logMsg + ", 催签收处理开始");
        // 圆钉提醒
        DataResult dataResult = null;
        YuanDingImMessageRequest yuanDingImMessageRequest = new YuanDingImMessageRequest();
        dataResult = wdgjImMessageMsUtil.urgePushMsg(yuanDingImMessageRequest, follow.getBusinessId(), follow.getWaybillNo(), follow.getBusinessDt(), follow.getBusinessSourceName());
        log.info(logMsg + ", 催签收处理,圆钉提醒,结果:{}", JacksonUtil.toJson(dataResult));
        // 插入一条轨迹
        List<String> stationInfo = wdgjImMessageMsUtil.getStationInfo(follow.getBusinessId(), follow.getBusinessDt());
        if (dataResult != null && dataResult.getStatus() == 0 && !CollectionUtils.isEmpty(stationInfo)) {
            boolean trackInsertResult = trackUtil.urgeTrack(follow, UrgeFollowTraceSourceTypeEnum.URGE_FOLLOW_AI_NOTICE_10H_KF, String.join(",", stationInfo), "");
            log.info(logMsg + ",添加轨迹=" + (trackInsertResult ? "成功" : "失败"));
        }
    }
}

三、 引入策略类, 根据工单类型选择具体实现

java 复制代码
    @Autowired
    private final Map<String, ProblemStrategy> strategyMap = new HashMap<>();

    public ProblemStrategy getProblemStrategy (Integer smallProblemId) {
        for (String key : strategyMap.keySet()) {
            if (smallProblemId.equals(strategyMap.get(key).getSmallProblemId())) {
                return strategyMap.get(key);
            }
        }
        return null;
    }
java 复制代码
            // 4、未完结,根据工单类型做对应处理
            ProblemStrategy problemStrategy = getProblemStrategy(follow.getSmallProblemId());
            if (problemStrategy != null) {
                problemStrategy.handle(follow, logMsg);
            }

一个策略模式就搞定了。

相关推荐
练习时长一年2 分钟前
AopAutoConfiguration源码阅读
java·spring boot·intellij-idea
阿巴~阿巴~23 分钟前
冒泡排序算法
c语言·开发语言·算法·排序算法
源码宝1 小时前
【智慧工地源码】智慧工地云平台系统,涵盖安全、质量、环境、人员和设备五大管理模块,实现实时监控、智能预警和数据分析。
java·大数据·spring cloud·数据分析·源码·智慧工地·云平台
看到我,请让我去学习2 小时前
QT - QT开发进阶合集
开发语言·qt
weixin_307779132 小时前
VS Code配置MinGW64编译SQLite3库
开发语言·数据库·c++·vscode·算法
David爱编程2 小时前
面试必问!线程生命周期与状态转换详解
java·后端
LKAI.3 小时前
传统方式部署(RuoYi-Cloud)微服务
java·linux·前端·后端·微服务·node.js·ruoyi
HeyZoeHey3 小时前
Mybatis执行sql流程(一)
java·sql·mybatis
2301_793086873 小时前
SpringCloud 07 微服务网关
java·spring cloud·微服务
励志不掉头发的内向程序员3 小时前
STL库——string(类函数学习)
开发语言·c++