策略模式-java实现

策略模式

以支付为例,用户在支付商品时,可以有多种支付方式,如支付宝,微信,现金。当支付方式增多,支付策略复杂化,我们就要考虑使用策略模式,这样可以避免if else 成堆出现,既不美观也很容易出错。

假设目前支持支付宝,微信,现金支付。有如下不同支付优惠

支付方式支付优惠策略
支付宝满100,每10元减1元。不满100 ,98折扣
微信全部98折扣
现金无优惠
  1. 定义支付接口

    public interface Pay {
        void pay(Integer amount);
    }
    
  2. 实现支付接口(不同的支付方式有不同的策略)

    支付宝支付

    @Component
    public class AlPay implements Pay {
        @Override
        public void pay(Integer amount) {
            //实现满10减1
            if (amount >= 100) {
                int count = amount / 10;
                System.out.println("支付宝支付" + (amount - count));
            }else {
                System.out.println("支付宝支付" + amount*0.98);
            }
        }
    }
    

    微信支付

    @Component
    public class WxPay implements Pay {
        @Override
        public void pay(Integer amount) {
            System.out.println("微信支付" + amount * 0.98);
        }
    }
    

    现金支付

    @Component
    public class CashPay implements Pay {
        @Override
        public void pay(Integer amount) {
            System.out.println("现金支付" + amount);
        }
    }
    
  3. 定义支付枚举

    这里有个要求是payName必须和Bean的名字一致。

    通过枚举的ordinal 我们知道AlPay是0,WxPay是1,CashPay是2,我们就建立了一对一关系,只要给我数字这里就可以通过getPayTypeEnum()返回对应的枚举

    @Getter
    @AllArgsConstructor
    public enum PayTypeEnum {
        AlPay("alPay"),
        WxPay("wxPay"),
        CashPay("cashPay");
    
        private final String payName;
    
        public static PayTypeEnum getPayTypeEnum(String ordinal) {
            return Arrays.stream(PayTypeEnum.values())
                    .filter(payTypeEnum -> String.valueOf(payTypeEnum.ordinal()).equals(ordinal))
                    .findFirst().orElseThrow(() -> new IllegalArgumentException("支付类型不存在"));
        }
    }
    
  4. Controller

    接收支付类型和支付金额

    @RestController
    public class TestStrategyController {
    
        @Autowired
        private PayStrategyContext payStrategyContext;
    
        @RequestMapping("/pay")
        public void get(@RequestParam("type") String type, 
                        @RequestParam("amount") String amount) {
            //通过数字获取对应的支付枚举对象
            PayTypeEnum payTypeEnum = PayTypeEnum.getPayTypeEnum(type);
            Pay pay = payStrategyContext.getPayType(payTypeEnum);
            pay.pay(Integer.valueOf(amount));
        }
    
    }
    
  5. 支付策略上下文

    通过spring依赖注入Map型,Key为Bean的名字,Value为现实类。通过Map的get方法提供Bean的名字,获取对应的支付现实类。

    @Component
    public class PayStrategyContext {
    
        @Resource
        private Map<String, Pay> maps;
    
        public Pay getPayType(PayTypeEnum payTypeEnum) {
            return maps.get(payTypeEnum.getPayName());
        }
    }
    
  • 20
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值