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

策略模式结合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";
    }

相关文章:

  • 学生信息管理系统C++
  • ldap协议(常用于统一身份认证)与dict协议(在线词典)
  • 深入解析JVM堆内存管理:对象流转与优化策略全揭秘
  • Day40 代码随想录打卡|二叉树篇---完全二叉树的节点个数
  • 【linux】(2)文件内容排序sort
  • 【射击game】
  • WebGL开发三维家装设计
  • python采集晋江文学城小说数据
  • mybatis配置环境流程
  • 教务管理系统带万字文档基于springboot+vue的校务管理系统java项目
  • 16:00面试,16:08就出来了,问的问题有点变态。。。
  • 硬币检测电路设计
  • C++ 的 Tag Dispatching(标签派发) 惯用法
  • 深入Netty RPC内核:编码、通信与性能优化全指南
  • cv2函数实践-图像处理(中心外扩的最佳RoI/根据两个坐标点求缩放+偏移后的RoI/滑窗切片/VOC的颜色+调色板)
  • godot.bk:how to add map to the game
  • vruntime
  • 阿里云 通过EIP实现VPC下的SNAT以及DNAT
  • echarts绘制三维柱状图
  • 数据结构:队列
  • 国家主席习近平同普京总统签署关于进一步深化中俄新时代全面战略协作伙伴关系的联合声明
  • 超燃!走过莫斯科街头的“中国排面”
  • 湖南张家界警方公告宣布一名外国人居留许可作废
  • 央视315晚会曝光“保水虾仁”后,湛江4家涉事企业被罚超800万元
  • 湖北十堰市委副秘书长管聪履新丹江口市代市长
  • 上海虹桥机场至北京首都机场快线试运行跨航司自愿签转服务