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

一个策略模式就搞定了。

相关推荐
GottdesKrieges41 分钟前
OceanBase数据库磁盘空间管理
java·数据库·oceanbase
Themberfue2 小时前
Redis ⑦-set | Zset
java·开发语言·数据库·redis·sql·缓存
__lost3 小时前
MATLAB画出3d的常见复杂有机分子和矿物的分子结构
开发语言·人工智能·matlab·化学·分子结构
夜夜敲码4 小时前
C语言教程(十八):C 语言共用体详解
c语言·开发语言
大学生亨亨4 小时前
go语言八股文(五)
开发语言·笔记·golang
raoxiaoya4 小时前
同时安装多个版本的golang
开发语言·后端·golang
此木|西贝5 小时前
【设计模式】享元模式
java·设计模式·享元模式
cloues break.6 小时前
C++进阶----多态
开发语言·c++
我不会编程5556 小时前
Python Cookbook-6.10 保留对被绑定方法的引用且支持垃圾回收
开发语言·python