背景:
下单时有很多情况,有的是需要唤起支付,有的不需要支付,这样就需要写很多冗余代码,下面使用策略模式优化这种情况
代码结构
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.*;
-
- @RestController
- @RequestMapping("/orders")
- public class OrderController {
-
- @Autowired
- private OrderServiceImpl orderService;
-
- @PostMapping("/create/free")
- public OrderVO createFreeOrder(@RequestBody FreeOrderDTO freeOrderDTO) {
- freeOrderDTO.setOrderType("freeOrder");
- return orderService.createOrder(freeOrderDTO);
- }
-
- @PostMapping("/create/paid")
- public OrderVO createPaidOrder(@RequestBody PaidOrderDTO paidOrderDTO) {
- paidOrderDTO.setOrderType("paidOrder");
- return orderService.createOrder(paidOrderDTO);
- }
- }
- import org.springframework.stereotype.Service;
-
- import javax.annotation.Resource;
-
- @Service
- public class OrderServiceImpl {
-
- @Resource
- private OrderCreationStrategyFactory strategyFactory;
-
- public OrderVO createOrder(OrderDTO orderDTO) {
- String orderType = orderDTO.getOrderType();
- OrderCreationStrategy extends OrderDTO> strategy = strategyFactory.getStrategy(orderType);
- if (strategy == null) {
- throw new IllegalArgumentException("No strategy found for order type: " + orderType);
- }
- return strategy.createOrder(orderDTO);
- }
- }
- import lombok.Data;
-
- @Data
- public class OrderVO {
- private String orderId;
- private String status;
- private double amount;
- }
- import lombok.Data;
-
- @Data
- public abstract class OrderDTO {
- private String orderType;
- }
-
- @Data
- public class FreeOrderDTO extends OrderDTO {
- private String freeOrderDetail;
- }
-
- @Data
- public class PaidOrderDTO extends OrderDTO {
- private double amount;
- private String paidOrderDetail;
- }
策略类接口
- public interface OrderCreationStrategy
extends OrderDTO> { - OrderVO createOrder(T orderDTO);
- }
免密支付策略类
- import org.springframework.stereotype.Component;
-
- @Component
- public class FreeOrderCreationStrategy implements OrderCreationStrategy
{ -
- @Override
- public OrderVO createOrder(FreeOrderDTO orderDTO) {
- OrderVO orderVO = new OrderVO();
- orderVO.setOrderId(UUID.randomUUID().toString());
- orderVO.setStatus("FREE_ORDER_CREATED");
- orderVO.setAmount(0);
- return orderVO;
- }
- }
需要支付的策略类
- import org.springframework.stereotype.Component;
-
- @Component
- public class PaidOrderCreationStrategy implements OrderCreationStrategy
{ -
- @Override
- public OrderVO createOrder(PaidOrderDTO orderDTO) {
- OrderVO orderVO = new OrderVO();
- orderVO.setOrderId(UUID.randomUUID().toString());
- orderVO.setStatus("PAID_ORDER_CREATED");
- orderVO.setAmount(orderDTO.getAmount());
- return orderVO;
- }
- }
策略类工厂
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
-
- import javax.annotation.PostConstruct;
- import java.util.Map;
- import java.util.concurrent.ConcurrentHashMap;
-
- @Component
- public class OrderCreationStrategyFactory {
-
- private static final Map
extends OrderDTO>> strategies = new ConcurrentHashMap<>(); -
- @Autowired
- private FreeOrderCreationStrategy freeOrderCreationStrategy;
-
- @Autowired
- private PaidOrderCreationStrategy paidOrderCreationStrategy;
-
- @PostConstruct
- public void init() {
- strategies.put("freeOrder", freeOrderCreationStrategy);
- strategies.put("paidOrder", paidOrderCreationStrategy);
- }
-
- public OrderCreationStrategy extends OrderDTO> getStrategy(String strategyType) {
- return strategies.get(strategyType);
- }
- }