OrderStrategyFactory.java 1.2 KB

12345678910111213141516171819202122232425262728293031
  1. package strategyPattern.factory;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import strategyPattern.enums.OrderType;
  5. import strategyPattern.service.OrderStrategy;
  6. import strategyPattern.service.impl.GroupBuyOrderStrategy;
  7. import strategyPattern.service.impl.NormalOrderStrategy;
  8. import strategyPattern.service.impl.SeckillOrderStrategy;
  9. import java.util.EnumMap;
  10. import java.util.HashMap;
  11. import java.util.Map;
  12. public class OrderStrategyFactory {
  13. private static final Logger log = LoggerFactory.getLogger(OrderStrategyFactory.class);
  14. public static Map<String, OrderStrategy> STRATEGY_MAP = new HashMap<>();
  15. static {
  16. STRATEGY_MAP.put(OrderType.GROUPBY.getType(), new GroupBuyOrderStrategy());
  17. STRATEGY_MAP.put(OrderType.NORMAL.getType(), new NormalOrderStrategy());
  18. STRATEGY_MAP.put(OrderType.SECKILL.getType(), new SeckillOrderStrategy());
  19. }
  20. public static OrderStrategy getStrategy(String type) throws Exception {
  21. log.info("STRATEGY_MAP: " + STRATEGY_MAP);
  22. OrderStrategy o = STRATEGY_MAP.getOrDefault(type, null);
  23. if (o == null) {
  24. throw new Exception("没有对应的下单策略");
  25. }
  26. return o;
  27. }
  28. }