12345678910111213141516171819202122232425262728293031 |
- package strategyPattern.factory;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import strategyPattern.enums.OrderType;
- import strategyPattern.service.OrderStrategy;
- import strategyPattern.service.impl.GroupBuyOrderStrategy;
- import strategyPattern.service.impl.NormalOrderStrategy;
- import strategyPattern.service.impl.SeckillOrderStrategy;
- import java.util.EnumMap;
- import java.util.HashMap;
- import java.util.Map;
- public class OrderStrategyFactory {
- private static final Logger log = LoggerFactory.getLogger(OrderStrategyFactory.class);
- public static Map<String, OrderStrategy> STRATEGY_MAP = new HashMap<>();
- static {
- STRATEGY_MAP.put(OrderType.GROUPBY.getType(), new GroupBuyOrderStrategy());
- STRATEGY_MAP.put(OrderType.NORMAL.getType(), new NormalOrderStrategy());
- STRATEGY_MAP.put(OrderType.SECKILL.getType(), new SeckillOrderStrategy());
- }
- public static OrderStrategy getStrategy(String type) throws Exception {
- log.info("STRATEGY_MAP: " + STRATEGY_MAP);
- OrderStrategy o = STRATEGY_MAP.getOrDefault(type, null);
- if (o == null) {
- throw new Exception("没有对应的下单策略");
- }
- return o;
- }
- }
|