策略模式 + 工厂模式 + 门面模式 实现用户多类型支付功能

博客围绕用户支付需求,运用策略模式、工厂模式和门面模式实现支付功能。目前支持支付宝和微信支付,未来会新增方式。详细介绍了新建策略类、创建策略工厂、上下文角色等步骤,最后创建 Controller 层调用并进行 postman 测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

1.新建策略类

2.创建策略工厂

3.创建策略的上下文角色

4.提供统一的访问入口

5.创建 Controller 层,进行调用

​​​​​​​6.进行postman测试


 

  • 需求:用户进行支付
  • 1.目前支持支付宝支付和微信支付,未来会新增银行卡,余额等方式。使用策略模式,每一种支付方式都是一种策略。
  • 2.根据用户传入的支付类型,创建不同的策略类,使用工厂模式。
  • 3.封装一个facade的jar包,其他系统直接通过一个统一的入口,进行该功能的调用,使用门面模式。

1.新建策略类

@Service
public interface PayStrategy {
    Boolean pay(Paybody paybody);
}

public class WxPayStrategy implements PayStrategy {
    @Override
    public Boolean pay(Paybody paybody) {
        // 模拟微信支付过程
        System.out.println("微信支付失败!");
        return false;
    }
}

public class ZfbPayStrategyPay implements PayStrategy{
    @Override
    public Boolean pay(Paybody paybody) {
        // 模拟支付宝支付过程
        System.out.println("支付宝支付成功!");
        return true;
    }
}

@Data
public class Paybody {
    /**
     * 支付类型
     * ZFB  支付宝
     * WX   微信
     */
    private String type;
    /**
     * 商品
     */
    private String product;
}

2.创建策略工厂

/**
 * 支付策略工厂
 */
public class StrategyFactory {
    public static PayStrategy getPayStrategy(String type){
        // 1.通过枚举中的type获取对应value
        String value = EnumUtil.getFieldBy(PayStrategyEnum::getValue, PayStrategyEnum::getType, type);
        // 2.使用反射创建对应的策略类
        PayStrategy payStrategy = ReflectUtil.newInstance(value);
        return payStrategy;
    }
}

/**
 * 支付策略枚举
 */
public enum PayStrategyEnum {
    ZFB("ZFB","com.example.demo.pay.stragety.ZfbPayStrategyPay"),
    WX("WX","com.example.demo.pay.stragety.WxPayStrategy")
    ;
    String type;
    String value;
    PayStrategyEnum(String type,String value){
        this.type = type;
        this.value = value;
    }

    public String getValue() {
        return value;
    }

    public String getType() {
        return type;
    }

}

3.创建策略的上下文角色

起承上启下封装作用,屏蔽高层模块对策略、算法的直接访问,封装可能存在的变化。

public class PayContext {
    private PayStrategy payStrategy;

    public PayContext(PayStrategy payStrategy) {
        this.payStrategy = payStrategy;
    }

    public Boolean execute(Paybody paybody) {
        return this.payStrategy.pay(paybody);
    }
}

4.提供统一的访问入口

public class StrategyFacade {
    public static Boolean Pay(Paybody paybody){
        // 1.获取策略对象
        PayStrategy payStrategy = StrategyFactory.getPayStrategy(paybody.getType());
        // 2.获取策略上下文
        PayContext payContext = new PayContext(payStrategy);
        // 3.进行扣款
        return payContext.execute(paybody);
    }
}

5.创建 Controller 层,进行调用

@RestController
public class PayController {

    @Autowired
    private PayService payService;

    @PostMapping("/pay")
    public Boolean pay(@RequestBody Paybody paybody){
        return payService.Pay(paybody);
    }
}


@Service
public class PayService {
    public Boolean Pay(Paybody paybody){
        if (!EnumUtil.contains(PayStrategyEnum.class, paybody.getType())) {
            throw new IllegalArgumentException("不支持的支付方式!");
        }
        return StrategyFacade.Pay(paybody);
    }
}

6.进行postman测试

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值