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

一个策略模式就搞定了。

相关推荐
云雀衔光5 小时前
MCP 协议全景:为什么它是 AI 连接工具的「USB-C」
java·开发语言·数据库·人工智能·ai编程
ly76895 小时前
Spring 异步编程的隐藏风险:@Async 线程池耗尽与异常处理详解
java·后端·spring·异常处理·线程池·任务拒绝
用户8181870627465 小时前
第23章 热点Key问题排查与解决:大促场景经典坑
java·后端
geovindu5 小时前
go: Task Scheduler
开发语言·后端·golang
IT枫斗者枫哥5 小时前
Java 接口超时后,重试为什么会多建一条任务?
java
用户3126874877205 小时前
CAS 到底是怎么保证原子性的?从 Unsafe 到 VarHandle 的演进
java
Zane19945 小时前
线上突然OOM,你的排查顺序是先看日志还是先重启
java·后端
会飞的拖把5 小时前
Python 多任务编程:并发、进程、线程与线程安全详解
开发语言·python
君顾15 小时前
折扣卡 CPS 小程序定制:从业务流程到系统架构的实践指南
java·开发语言·折扣卡
见不散5 小时前
GPT-5.6 Luna (Batch) 批量处理能力深度评测
java·gpt·batch