欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 文化 > 策略模式结合SpringBoot

策略模式结合SpringBoot

2025/2/26 9:40:36 来源:https://blog.csdn.net/qq_30609633/article/details/145852585  浏览:    关键词:策略模式结合SpringBoot

一 定义策略类接口

在这里插入图片描述


import org.jetbrains.annotations.NotNull;import javax.annotation.Nullable;public interface ApprovalStrategy<T> {/*** 返回需要的类型 不能为空** @return*/@NotNullClass<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> {@Overridepublic void handlePass(Integer businessId, @Nullable String comment) {}@Overridepublic void handleReject(Integer businessId, String comment) {}@NotNull@Overridepublic 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);
这里可以用个枚举 来规范调用

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词