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


    背景:

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

    代码结构

    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. }

  • 相关阅读:
    【OpenCV入门】第五部分——图像运算
    配置Nginx和其他应用的HTTPS访问
    MySQL——事务(说明及其细节)
    大漠插件的使用过程(一、插件的注册)
    MT4到达压力位支撑位自动发送微信消息提醒
    搜广推火线入门
    FreeRTOS 延时函数和软件定时器 详解
    PyTorch 被大量网友反馈,TorchRec 这一新库“诞生”且规模宏大
    [附源码]计算机毕业设计springboot基于JAVA技术的旅游信息交互系统
    Hadoop核心之MapReduce案例总结
  • 原文地址:https://blog.csdn.net/qq_40704513/article/details/138872110