【Spring-boot】Spring实现策略模式

  1. PaymentStrategyManager
package com.lh.demo.service;

import com.lh.demo.domain.Order;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author: fenda
 * @createDate: 2022/11/16 11:26
 * @version: 1.0
 * @description:
 */
@Slf4j
@Component
public class PaymentStrategyManager {

    private final Map<String, PaymentStrategy> processMap = new ConcurrentHashMap<>();

    public void registerHandler(PaymentStrategy strategy) {
        if (processMap.containsKey(strategy.key())) {
            log.error("Key is already in use! key={}", strategy.key());
            throw new RuntimeException("Key is already in use! key=" + strategy.key());
        }
        processMap.put(strategy.key(), strategy);
    }

    public void work(Order order) {
        PaymentStrategy paymentStrategy = processMap.get(order.getPaymentType());
        if (paymentStrategy != null) {
            paymentStrategy.pay(order);
        }
    }
}

  1. PaymentStrategy
package com.lh.demo.service;

import com.lh.demo.domain.Order;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;

/**
 * @author: fenda
 * @createDate: 2022/11/16 11:17
 * @version: 1.0
 * @description:
 */
public abstract class PaymentStrategy {

    @Autowired
    private PaymentStrategyManager manager;

    @PostConstruct
    public void init() {
        manager.registerHandler(this);
    }


    public abstract String key();

    public abstract void pay(Order order);
}

  1. WxPaymentService
package com.lh.demo.service;

import com.lh.demo.domain.Order;
import com.lh.demo.domain.PaymentType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author: fenda
 * @createDate: 2022/11/16 14:25
 * @version: 1.0
 * @description:
 */
@Slf4j
@Service
public class WxPaymentService extends PaymentStrategy {

    @Override
    public String key() {
        return PaymentType.WX.getCode();
    }

    @Override
    public void pay(Order order) {
        log.info("wx pay ...");
        // TODO 支付逻辑
    }
}

  1. AlPaymentService
package com.lh.demo.service;

import com.lh.demo.domain.Order;
import com.lh.demo.domain.PaymentType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author: fenda
 * @createDate: 2022/11/16 14:27
 * @version: 1.0
 * @description:
 */
@Slf4j
@Service
public class AlPaymentService extends PaymentStrategy {

    @Override
    public String key() {
        return PaymentType.AL.getCode();
    }

    @Override
    public void pay(Order order) {
        log.info("ali pay ...");
        // TODO 支付逻辑
    }
}

  1. YsfPaymentService
package com.lh.demo.service;

import com.lh.demo.domain.Order;
import com.lh.demo.domain.PaymentType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author: fenda
 * @createDate: 2022/11/16 14:29
 * @version: 1.0
 * @description:
 */
@Slf4j
@Service
public class YsfPaymentService extends PaymentStrategy {
    @Override
    public String key() {
        return PaymentType.YSF.getCode();
    }

    @Override
    public void pay(Order order) {
        log.info("ysf pay ...");
        // TODO 支付逻辑
    }
}

  1. PaymentType
package com.lh.demo.domain;

import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * @author: fenda
 * @createDate: 2022/11/16 11:23
 * @version: 1.0
 * @description:
 */
@Getter
@AllArgsConstructor
public enum PaymentType {
    WX("WX", "微信"),
    AL("AL", "支付宝"),
    YSF("YSF", "云闪付");
    public final String code;
    public final String msg;

}

  1. Order
package com.lh.demo.domain;

import lombok.Data;

import java.math.BigDecimal;

/**
 * @author: fenda
 * @createDate: 2022/11/16 11:21
 * @version: 1.0
 * @description:
 */
@Data
public class Order {

    /**
     * 支付方式
     */
    private String paymentType;

    /**
     * 金额
     */
    private BigDecimal amt;
}

  1. PaymentController
package com.lh.demo.controller;

import com.lh.demo.domain.Order;
import com.lh.demo.service.PaymentStrategyManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: fenda
 * @createDate: 2022/11/16 14:31
 * @version: 1.0
 * @description:
 */
@RestController
public class PaymentController {

    @Autowired
    private PaymentStrategyManager manager;

    @PostMapping("/pay")
    public String pay(@RequestBody Order order) {
        manager.work(order);
        return "ok";
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值