策略模式结合Spring使用

1.抽象策略

/**
 * 支付方式策略
 * @author Linging
 * @version 1.0.0
 * @since 1.0
 */
public interface PayStrategy {

    void pay(BigDecimal money);

}

2.具体策略

/**
 * 支付宝
 * @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 + " 元");
    }
}
/**
 * 微信支付
 * @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 + " 元");
    }
}
/**
 * 信用卡支付
 * @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.工厂配置策略

/**
 * 配置策略
 * @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.枚举

/**
 * @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.使用

    @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";
    }

相关推荐

  1. 策略模式结合Spring使用

    2024-06-07 02:44:05       26 阅读
  2. 使用策略模式实现 Spring 分布式和单机限流

    2024-06-07 02:44:05       35 阅读
  3. 设计模式-策略模式-使用

    2024-06-07 02:44:05       32 阅读
  4. Spring设计模式-实战篇之策略模式 + 工厂模式

    2024-06-07 02:44:05       47 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-07 02:44:05       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-07 02:44:05       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-07 02:44:05       87 阅读
  4. Python语言-面向对象

    2024-06-07 02:44:05       96 阅读

热门阅读

  1. Scala编程基础4 类、对象、继承、特质

    2024-06-07 02:44:05       30 阅读
  2. [React]用 flushSync 同步更新 state

    2024-06-07 02:44:05       33 阅读
  3. Sass 详解

    2024-06-07 02:44:05       26 阅读
  4. linux c socket编程里SO_REUSEADDR的作用

    2024-06-07 02:44:05       34 阅读
  5. 【安卓基础】-- 消息机制 Handler

    2024-06-07 02:44:05       32 阅读
  6. lm studio 0.2.24国内下载模型

    2024-06-07 02:44:05       32 阅读
  7. 常见代码版本管理工具

    2024-06-07 02:44:05       33 阅读
  8. Android WebView升级

    2024-06-07 02:44:05       22 阅读
  9. 判断素数/质数

    2024-06-07 02:44:05       29 阅读
  10. Loguru,一个 Python 日志神器

    2024-06-07 02:44:05       31 阅读
  11. MySQL学习——从命令行调用MySQL 程序

    2024-06-07 02:44:05       26 阅读