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

背景:

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

代码结构

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<T extends OrderDTO> {
    OrderVO createOrder(T orderDTO);
}

免密支付策略类 

import org.springframework.stereotype.Component;

@Component
public class FreeOrderCreationStrategy implements OrderCreationStrategy<FreeOrderDTO> {

    @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<PaidOrderDTO> {

    @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<String, OrderCreationStrategy<? 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);
    }
}

  • 8
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值