• 设计模式之策略模式(一)


    背景:

    下单时有很多情况,有的是需要唤起支付,有的不需要支付,这样就需要写很多冗余代码,下面使用策略模式优化这种情况

    代码结构

    1. import org.springframework.beans.factory.annotation.Autowired;
    2. import org.springframework.web.bind.annotation.*;
    3. @RestController
    4. @RequestMapping("/orders")
    5. public class OrderController {
    6. @Autowired
    7. private OrderServiceImpl orderService;
    8. @PostMapping("/create/free")
    9. public OrderVO createFreeOrder(@RequestBody FreeOrderDTO freeOrderDTO) {
    10. freeOrderDTO.setOrderType("freeOrder");
    11. return orderService.createOrder(freeOrderDTO);
    12. }
    13. @PostMapping("/create/paid")
    14. public OrderVO createPaidOrder(@RequestBody PaidOrderDTO paidOrderDTO) {
    15. paidOrderDTO.setOrderType("paidOrder");
    16. return orderService.createOrder(paidOrderDTO);
    17. }
    18. }
    1. import org.springframework.stereotype.Service;
    2. import javax.annotation.Resource;
    3. @Service
    4. public class OrderServiceImpl {
    5. @Resource
    6. private OrderCreationStrategyFactory strategyFactory;
    7. public OrderVO createOrder(OrderDTO orderDTO) {
    8. String orderType = orderDTO.getOrderType();
    9. OrderCreationStrategyextends OrderDTO> strategy = strategyFactory.getStrategy(orderType);
    10. if (strategy == null) {
    11. throw new IllegalArgumentException("No strategy found for order type: " + orderType);
    12. }
    13. return strategy.createOrder(orderDTO);
    14. }
    15. }
    1. import lombok.Data;
    2. @Data
    3. public class OrderVO {
    4. private String orderId;
    5. private String status;
    6. private double amount;
    7. }
    1. import lombok.Data;
    2. @Data
    3. public abstract class OrderDTO {
    4. private String orderType;
    5. }
    6. @Data
    7. public class FreeOrderDTO extends OrderDTO {
    8. private String freeOrderDetail;
    9. }
    10. @Data
    11. public class PaidOrderDTO extends OrderDTO {
    12. private double amount;
    13. private String paidOrderDetail;
    14. }

    策略类接口 

    1. public interface OrderCreationStrategyextends OrderDTO> {
    2. OrderVO createOrder(T orderDTO);
    3. }

    免密支付策略类 

    1. import org.springframework.stereotype.Component;
    2. @Component
    3. public class FreeOrderCreationStrategy implements OrderCreationStrategy {
    4. @Override
    5. public OrderVO createOrder(FreeOrderDTO orderDTO) {
    6. OrderVO orderVO = new OrderVO();
    7. orderVO.setOrderId(UUID.randomUUID().toString());
    8. orderVO.setStatus("FREE_ORDER_CREATED");
    9. orderVO.setAmount(0);
    10. return orderVO;
    11. }
    12. }

    需要支付的策略类 

    1. import org.springframework.stereotype.Component;
    2. @Component
    3. public class PaidOrderCreationStrategy implements OrderCreationStrategy {
    4. @Override
    5. public OrderVO createOrder(PaidOrderDTO orderDTO) {
    6. OrderVO orderVO = new OrderVO();
    7. orderVO.setOrderId(UUID.randomUUID().toString());
    8. orderVO.setStatus("PAID_ORDER_CREATED");
    9. orderVO.setAmount(orderDTO.getAmount());
    10. return orderVO;
    11. }
    12. }

    策略类工厂 

    1. import org.springframework.beans.factory.annotation.Autowired;
    2. import org.springframework.stereotype.Component;
    3. import javax.annotation.PostConstruct;
    4. import java.util.Map;
    5. import java.util.concurrent.ConcurrentHashMap;
    6. @Component
    7. public class OrderCreationStrategyFactory {
    8. private static final Mapextends OrderDTO>> strategies = new ConcurrentHashMap<>();
    9. @Autowired
    10. private FreeOrderCreationStrategy freeOrderCreationStrategy;
    11. @Autowired
    12. private PaidOrderCreationStrategy paidOrderCreationStrategy;
    13. @PostConstruct
    14. public void init() {
    15. strategies.put("freeOrder", freeOrderCreationStrategy);
    16. strategies.put("paidOrder", paidOrderCreationStrategy);
    17. }
    18. public OrderCreationStrategyextends OrderDTO> getStrategy(String strategyType) {
    19. return strategies.get(strategyType);
    20. }
    21. }

  • 相关阅读:
    Debezium报错处理系列之九十九:ConnectException: Source offset ‘file‘ parameter is missing
    暑期JAVA学习(42.2)TCP通信实战案例——模拟BS系统
    山东大学项目实训(二十七)—— 微信小程序开发总结,一年时间真的可以改变一个人很多
    Java vo dto 使用场景
    阅读 | 001《人工智能导论》(一)绪论及知识表示篇
    MEMS传感器的原理与构造——单片式硅陀螺仪
    主流的CPU架构
    拒绝访问硬盘拒绝访问的找回方法
    Codeforces Round #814 (Div. 2) A - F
    springboot验证码的生成与验证
  • 原文地址:https://blog.csdn.net/qq_40704513/article/details/138872110