当前位置: 首页 > news >正文

策略模式结合SpringBoot

一 定义策略类接口

在这里插入图片描述


import org.jetbrains.annotations.NotNull;

import javax.annotation.Nullable;

public interface ApprovalStrategy<T> {

    /**
     * 返回需要的类型 不能为空
     *
     * @return
     */
    @NotNull
    Class<T> getSupportedType();

    void handlePass(T businessId, @Nullable String comment);

    void handleReject(T businessId, String comment);
}

二 定义Context

import lombok.AllArgsConstructor;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;

import java.util.Map;

@AllArgsConstructor
@Component
public class ApprovalContext {

    private final Map<String, ApprovalStrategy<?>> strategies;


    public <T> void executePass(String type, T businessId, @Nullable String comment) {
        ApprovalStrategy<T> strategy = getStrategyWithValidation(type);
        strategy.handlePass(convertBusinessId(strategy, businessId), comment);
    }


    public <T> void executeReject(String type, T businessId, String comment) {
        ApprovalStrategy<T> strategy = getStrategyWithValidation(type);
        strategy.handleReject(convertBusinessId(strategy, businessId), comment);
    }

    private <T> ApprovalStrategy<T> getStrategyWithValidation(String type) {
        @SuppressWarnings("unchecked")
        ApprovalStrategy<T> strategy = (ApprovalStrategy<T>) strategies.get(type);
        if (strategy == null) {
            throw new IllegalArgumentException("未知的审批类型:" + type);
        }
        return strategy;
    }

    private <T> T convertBusinessId(ApprovalStrategy<T> strategy, Object businessId) {
        Class<T> supportedType = strategy.getSupportedType();
        if (supportedType == null) {
            throw new NullPointerException("supportedType 不能为空");
        }
        if (!supportedType.isInstance(businessId)) {
            if (supportedType == Integer.class && businessId instanceof String) {
                return supportedType.cast(Integer.valueOf((String) businessId));
            } else {
                throw new IllegalArgumentException(
                        "不支持的类型转换:" + businessId.getClass() + " 到 " + supportedType
                );
            }
        }
        return supportedType.cast(businessId);
    }


}


三 实现类


@Component("TEST_REVIEW")
@RequiredArgsConstructor
public class TestApproval implements ApprovalStrategy<Integer> {

    @Override
    public void handlePass(Integer businessId, @Nullable String comment) {

    }

    @Override
    public void handleReject(Integer businessId, String comment) {

    }

    @NotNull
    @Override
    public Class<Integer> getSupportedType() {
        return Integer.class;
    }
}

这里TEST_REVIEW为ApprovalType枚举中的常量字符 利用spring将其收集到ApprovalContext类的strategies中

使用:

1 注入private final ApprovalContext approvalContext;
2 approvalContext.executePass(ApprovalType.TEST_REVIEW.name(), id, null);
|| approvalContext.executeReject(ApprovalType.TEST_REVIEW.name(), id, null);
这里可以用个枚举 来规范调用

http://www.dtcms.com/a/36691.html

相关文章:

  • 银行信贷业务解析:运营与数据双视角下的业务本质与技术支撑
  • C#连接sql server
  • 什么是SEO通俗准确的解释
  • angular轮播图
  • 交换机与路由器连接方式
  • 2.25力扣每日一题--设计内存分配器
  • 排序算法适合的场景
  • TCP,http,WebSocket
  • android aosp系统定制如何监控系统性能
  • 改进的Siddon算法与原算法的区别及具体改进
  • SSL/TLS 协议、SSL证书 和 SSH协议 的区别和联系
  • 中级软考笔记-基础知识-8-网络与信息安全
  • 基于SpringBoot的精品水果线上销售平台系统设计与实现(源码+SQL脚本+LW+部署讲解等)
  • react18自定义hook实现
  • 基于three进行数字孪生平台架构设计及应用实现
  • 【安卓逆向】逆向APP界面UI修改再安装
  • Git详解及常用命令
  • .NET Core MVC IHttpActionResult 设置Headers
  • 将 iOS 项目打包并运行到真实的 iPhone 设备
  • python实现基于文心一言大模型的sql小工具
  • 02.25 继承和多态
  • 【大模型系列】使用ollama本地运行千问2.5模型
  • [字节青训_AI对话框]SSE交互规范、自定义事件、前后端数据传递、状态监听、连接和断开详解
  • knife4j+springboot3.4异常无法正确展示文档
  • (论文)使用双频分析检测 AI 合成的语音
  • 【大厂AI实践】微软:基于预训练的自然语言生成在搜索与广告中的应用
  • 高一英语拓展词汇第二版
  • 基于ArcGIS Pro、R、INVEST等多技术融合下生态系统服务权衡与协同动态分析实践应用
  • Android构建系统 - 03 编译执行流程
  • Zemax OpticStudio 中的扩散器建模