SpringBoot支付回调枚举+策略+工厂模式

定义业务枚举

java 复制代码
@Getter
public enum BizTypePrefixEnum {

    PAYMENT("PMT", "消费"),
    RECHARGE("RCH", "充值"),
    WITHDRAWAL("WDW", "提现"),
    REFUND("RFD", "退款"),
    RED_PACKET_PAY("RP", "红包支付"),

    ;
    private final String code;

    private final String desc;


    BizTypePrefixEnum(String code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    public static BizTypePrefixEnum getStartsWith(String outTradeNo) {
        for (BizTypePrefixEnum bizTypePrefixEnum : BizTypePrefixEnum.values()) {
            if (outTradeNo.startsWith(bizTypePrefixEnum.getCode())) {
                return bizTypePrefixEnum;
            }
        }
        return null;
    }
}

定义策略接口

java 复制代码
public interface NotifyProcessHandler {

    /**
     * 支付回调参数
     *
     * @param req
     */
    void process(NotifyPaymentReq req);

    /**
     * 业务类型
     *
     * @return
     */
    BizTypePrefixEnum getBizType();

}

不同枚举业务,分别定义实现类

java 复制代码
@Slf4j
@Service
public class PaymentNotifyHandler implements NotifyProcessHandler {

    @Override
    public void process(NotifyPaymentReq req) {
        // todo 消费支付结果处理

    }

    @Override
    public BizTypePrefixEnum getBizType() {
        return BizTypePrefixEnum.PAYMENT;
    }
}
java 复制代码
@Slf4j
@Service
public class RechargeNotifyHandler implements NotifyProcessHandler {
    @Override
    public void process(NotifyPaymentReq req) {
        // todo 充值支付结果处理

    }

    @Override
    public BizTypePrefixEnum getBizType() {
        return BizTypePrefixEnum.RECHARGE;
    }
}

...等等其他枚举处理类

创建工厂,初始化加载策略类

java 复制代码
@Component
public class NotifyProcessFactory implements InitializingBean {

    @Autowired
    private List<NotifyProcessHandler> notifyProcessHandlerList;

    private final Map<BizTypePrefixEnum, NotifyProcessHandler> handlerMap = new HashMap<>();

    @Override
    public void afterPropertiesSet() throws Exception {
        for (NotifyProcessHandler notifyProcessHandler : notifyProcessHandlerList) {
            handlerMap.put(notifyProcessHandler.getBizType(), notifyProcessHandler);
        }
    }

    public NotifyProcessHandler getHandler(BizTypePrefixEnum bizType) {
        return handlerMap.get(bizType);
    }

}

注入工厂,实现处理策略

java 复制代码
   @Autowired
    private NotifyProcessFactory notifyProcessFactory;

   NotifyProcessHandler handler = notifyProcessFactory.getHandler(BizTypePrefixEnum.getStartsWith(req.getOutTradeNo()));
            handler.process(req);
相关推荐
码士集团小青1 小时前
SpringBoot 集成缓存性能之王 Caffeine
spring boot·后端·缓存
凤凰院凶涛QAQ2 小时前
《Java版数据结构 & 集合类剖析》Map & Set & 反射:“从哈希碰撞到红黑树转换,反射帮你扒开 HashMap 的桶数组看个究竟”
java·数据结构·算法
H.莓飛2 小时前
【数据结构】排序_OJ题(一):计数、桶与排序后的处理
java·开发语言·数据结构
wangchunyu1143 小时前
JAVA AQS与Lock实现原理(含CAS)
java·开发语言·word
w_zero_one4 小时前
算法--模拟
java·数据结构·算法
Javatutouhouduan4 小时前
阿里1658页Java面试题总结(含答案)
java·高并发·java面试·java面试题·后端开发·java程序员·java八股文
秋天的怀念_David4 小时前
算法一千零一夜 · 第十三夜 幻方,武侠世界里的数学迷宫
java·程序人生·算法
专业程序开发源5 小时前
springboot欧缇蔓月子中心管理系统82697-计算机课程设计、毕业设计
java·spring boot·后端·python·spring·django·课程设计
秋天的怀念_David5 小时前
算法一千零一夜 · 第十四夜 幻方与奇妙的二进制
java·程序人生·算法
thefool1122665 小时前
网络编程^
java