策略模式结合Spring使用

1.抽象策略

java 复制代码
/**
 * 支付方式策略
 * @author Linging
 * @version 1.0.0
 * @since 1.0
 */
public interface PayStrategy {

    void pay(BigDecimal money);

}

2.具体策略

java 复制代码
/**
 * 支付宝
 * @author Linging
 * @version 1.0.0
 * @since 1.0
 */
@Component("aliPayStrategy")
public class AliPayStrategy implements PayStrategy{
    @Override
    public void pay(BigDecimal money) {
        System.out.println("使用支付宝支付了 " + money + " 元");
    }
}
java 复制代码
/**
 * 微信支付
 * @author Linging
 * @version 1.0.0
 * @since 1.0
 */
@Component("weChatPayStrategy")
public class WeChatPayStrategy implements PayStrategy{
    @Override
    public void pay(BigDecimal money) {
        System.out.println("使用微信支付了 " + money + " 元");
    }
}
java 复制代码
/**
 * 信用卡支付
 * @author Linging
 * @version 1.0.0
 * @since 1.0
 */
@Component("creditCardPayStrategy")
public class CreditCardPayStrategy implements PayStrategy{
    @Override
    public void pay(BigDecimal money) {
        System.out.println("使用信用卡支付了 " + money + " 元");
    }
}

3.工厂配置策略

java 复制代码
/**
 * 配置策略
 * @author Linging
 * @version 1.0.0
 * @since 1.0
 */
@Component
public class PayContentFactory {

    @Resource
    private ApplicationContext context;

    private Map<Integer, PayStrategy> map = new HashMap<>();

    @PostConstruct
    public void init(){
        for (PayEnum payEnum : PayEnum.values()) {
            map.putIfAbsent(payEnum.getCode(),
                    context.getBean(payEnum.getBeanName(), PayStrategy.class));
        }
    }

    public PayStrategy getPayStrategyPay(PayEnum payEnum){
        return map.get(payEnum.getCode());
    }

}

4.枚举

java 复制代码
/**
 * @author Linging
 * @version 1.0.0
 * @since 1.0
 */
public enum PayEnum {

    ALI(1, "支付宝", "aliPayStrategy"),
    WECHAT(2, "微信", "weChatPayStrategy"),
    CREDIT_CARD(3, "信用卡", "creditCardPayStrategy"),
    ;

    int code;

    String remark;

    String beanName;

    PayEnum(int code, String remark, String beanName) {
        this.code = code;
        this.remark = remark;
        this.beanName = beanName;
    }

    public int getCode() {
        return code;
    }

    public String getRemark() {
        return remark;
    }

    public String getBeanName() {
        return beanName;
    }
}

5.使用

java 复制代码
    @Resource
    private PayContentFactory payContentFactory;

    @GetMapping("/testStrategy")
    public String testStrategy(){

        payContentFactory.getPayStrategyPay(PayEnum.ALI).pay(new BigDecimal("100"));
        payContentFactory.getPayStrategyPay(PayEnum.WECHAT).pay(new BigDecimal("99"));
        payContentFactory.getPayStrategyPay(PayEnum.CREDIT_CARD).pay(new BigDecimal("88"));

        return "ok";
    }
相关推荐
EatFan6 分钟前
2026 后端 AI 集成选型拆解:Spring AI 2.0、MCP 协议与 Agent 内嵌三条路径的决策要点
java·人工智能·spring
天远API1 小时前
零信任架构实战:基于天远运营商近3个月欠费次数构建自动化授信调额合规网关
java·人工智能·架构·自动化
SummerJokes1 小时前
Java 集合 List 全套实验|从基础增删改查到实体类存储
java·python·list
长不大的杨瑞杰2 小时前
Spring AI 企业级 RAG 实战:检索、重排、结构化输出与提示注入防护
java·人工智能·spring·llm·rag·springai
阿凤212 小时前
学习java---流程控制篇
java·开发语言·学习
IT研究室2 小时前
最新计算机毕业设计选题推荐-基于spring boot的房屋交易系统-网站-文档指导-Java-springboot
java·spring boot·课程设计
RuoyiOffice2 小时前
Java物联网项目拆解:设备接入之后,数据怎样真正用起来
java·vue.js·spring boot·物联网·iot·企业管理·ruoyi office
Rain的Java大神之路3 小时前
Java后端用Vue3+TS独立闭环之后,MyBatis-Plus该退到哪一层?聊聊2026全栈效能的责任边界
java·后端·ai编程
比特屿3 小时前
Spring AI 2.1实战(六):Tool Calling全链路,从@Tool 到人工确认
java·后端
海宇服务3 小时前
零信任架构实战:基于海宇对外投资历史查询服务构建自动化供应商关联主体合规审查网关
java·人工智能·架构·自动化