策略模式结合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";
    }
相关推荐
茶馆大橘3 分钟前
【黑马点评】已解决java.lang.NullPointerException异常
java·开发语言
星辰@Sea7 分钟前
服务注册中心对比及使用场景分析
java·云原生
马剑威(威哥爱编程)10 分钟前
除了递归算法,要如何优化实现文件搜索功能
java·开发语言·算法·递归算法·威哥爱编程·memoization
bug菌¹12 分钟前
滚雪球学SpringCloud[4.1讲]: Spring Cloud Gateway详解
java·spring cloud·微服务
bug菌¹16 分钟前
滚雪球学SpringCloud[4.2讲]: Zuul:Netflix API Gateway详解
spring·spring cloud·gateway
小哇66626 分钟前
spring-TransactionTemplate 编程式事务
数据库·spring
MuseLss32 分钟前
HashMap高频面试知识点
java·开发语言·哈希算法
tyler-泰勒34 分钟前
初始c++:入门基础(完结)
java·开发语言·c++
重生之我要进大厂1 小时前
LeetCode 876
java·开发语言·数据结构·算法·leetcode
_祝你今天愉快1 小时前
技术成神之路:设计模式(十四)享元模式
java·设计模式