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);
            }

一个策略模式就搞定了。

相关推荐
代码不停1 分钟前
Java中文件操作和IO
java
夏天的味道٥3 分钟前
IDEA 开发工具常用插件整理
java·ide·intellij-idea
勇者无畏40419 分钟前
基于 Spring AI Alibaba 搭建 Text-To-SQL 智能系统(初始化)
java·后端·spring
一枚懒人25 分钟前
Java的Lamdba语法和函数式编程理解
java
全栈软件开发28 分钟前
最新版T5友价互站网源码商城PHP源码交易平台 完整带手机版源码网系统源码
android·开发语言·php
Mos_x29 分钟前
关于我们的python日记本
开发语言·python
土豆南瓜饼33 分钟前
关于mybatis-plus的一些默认配置
java
Juchecar34 分钟前
Java示例:设计模式是如何在实战中“自然生长”出来
java·设计模式
能摆一天是一天36 分钟前
JAVA Function
java
The Sheep 202336 分钟前
Dotnet-Dapper的用法
java·开发语言