java在spring环境中使用策略模式

本文介绍了如何在Spring框架下,使用策略模式处理支持支付宝、微信支付、银联支付等多方支付方式的API,通过策略接口和具体实现类实现支付功能的扩展性。
摘要由CSDN通过智能技术生成

需求

我们最近要开发一个多方支付,支持支付宝支付、微信支付、银联支付、等等的多方,以后说不准还有applePay、等等多种方式进行支付。一开始写if else确实没毛病,但是仔细一想,好像不符合我们设计模式的开闭原则,下面就来看看我们在spring环境中如何使用策略模式

流程

一般来说我们的api是从controller进来的,然后service,然后进行策略模式的选择,然后进到策略接口,然就是具体的实现方法的:
api—>service—>strategyContext—>Strategy—>strategyService
在这里插入图片描述

controller

@RestController
@RequestMapping("/strategy")
public class StrategyController {
    @Autowired
    private PaymentStrategyContext context;
    @GetMapping("pay")
    public String pay(@RequestParam("type") String type, @RequestParam("amount") double amount){
         context.handle(type,amount);
        return "支付成功";
    }
}

context

@Component
@Slf4j
public class PaymentStrategyContext {
    /**
     * 策略集合
     */
    @Autowired
    private  Map<String, PaymentStrategy> paymentStrategyMap = new HashMap<>();

    private static Map<String, String> classStrategyMap = new HashMap<>();

    static {
        classStrategyMap.put("ylk", "unionPayStrategy");
        classStrategyMap.put("wx", "weChatPayStrategy");
        classStrategyMap.put("zfb", "aliPayStrategy");
    }


    public void handle(String type,double amount){
        String className = classStrategyMap.get(type);

        PaymentStrategy paymentStrategy = paymentStrategyMap.get(className);
        if (paymentStrategy==null){
            log.warn("找策略失败,没有这个策略:{}",paymentStrategy);
            throw new RuntimeException("找策略失败,没有这个策略");
        }
        paymentStrategy.pay(amount);

    }
}

策略接口(对扩展开放)

public interface PaymentStrategy {
    // 支付
    void pay(double amount);

    // 或者其他业务...
}

具体实现类

@Service
public class AliPayStrategy implements PaymentStrategy {
    @Override
    public void pay(double amount) {
        // 调用支付宝支付接口
        // ...
        System.out.println("使用支付宝支付:" + amount + "元");
    }
}

/**
 * 银联卡支付策略
 */
@Service
public class UnionPayStrategy implements PaymentStrategy {
    @Override
    public void pay(double amount) {
        // 调用银联支付接口
        // ...
        System.out.println("使用银联支付:" + amount + "元");
    }
}

/**
 * 微信支付策略
 */
@Service
public class WeChatPayStrategy implements PaymentStrategy {
    @Override
    public void pay(double amount) {
        // 调用微信支付接口
        // ...
        System.out.println("使用微信支付:" + amount + "元");
    }
}

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值