• 【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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    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";
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
  • 相关阅读:
    非自交任意多边形与矩形框的交集面积计算方法
    spring7:总结56
    Activity的最佳实践
    2022年笔试知识总结展望(前后端均有)
    集成crawlergo和xray的src漏洞挖掘利器(hscan)
    java验证 Map 的 key、value 是否可以为空
    C++数据结构 -- AVL树
    配置centos 4.4.7 服务器(5)
    Buran勒索病毒通过Microsoft Excel Web查询文件进行传播
    场景应用:如何设计一个分布式系统?
  • 原文地址:https://blog.csdn.net/weixin_38538285/article/details/127885356