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

一个策略模式就搞定了。

相关推荐
L_09072 分钟前
【Linux】进程状态
linux·开发语言·c++
roman_日积跬步-终至千里2 分钟前
【Java并发】用 JMM 与 Happens-Before 解决多线程可见性与有序性问题
java·开发语言·spring
空空kkk2 分钟前
SSM项目练习——hami音乐(三)
java·数据库
2401_838472513 分钟前
C++异常处理最佳实践
开发语言·c++·算法
m0_736919106 分钟前
C++中的类型标签分发
开发语言·c++·算法
天桥下的卖艺者10 分钟前
使用R语言编写一个生成金字塔图形的函数
开发语言·数据库·r语言
爬山算法11 分钟前
Hibernate(78)如何在GraphQL服务中使用Hibernate?
java·hibernate·graphql
2301_7903009613 分钟前
C++与微服务架构
开发语言·c++·算法
独断万古他化16 分钟前
【Spring 核心:AOP】基础到深入:思想、实现方式、切点表达式与自定义注解全梳理
java·spring·spring aop·aop·切面编程
一切尽在,你来17 分钟前
C++多线程教程-1.1.4 并发编程的风险(竞态条件、死锁、数据竞争、资源争用)
开发语言·c++