code_hero преди 2 седмици
родител
ревизия
2c05c7ed0b

+ 3 - 1
.idea/vcs.xml

@@ -1,4 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <project version="4">
-  <component name="VcsDirectoryMappings" defaultProject="true" />
+  <component name="VcsDirectoryMappings">
+    <mapping directory="$PROJECT_DIR$" vcs="Git" />
+  </component>
 </project>

+ 14 - 0
src/main/java/strategyPattern/enums/OrderType.java

@@ -0,0 +1,14 @@
+package strategyPattern.enums;
+
+public enum OrderType {
+    GROUPBY("groupBy"),
+    NORMAL("normal"),
+    SECKILL("seckill");
+    String type;
+    OrderType(String type) {
+        this.type = type;
+    }
+    public String getType() {
+        return type;
+    }
+}

+ 31 - 0
src/main/java/strategyPattern/factory/OrderStrategyFactory.java

@@ -0,0 +1,31 @@
+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;
+    }
+}

+ 17 - 0
src/main/java/strategyPattern/main.java

@@ -0,0 +1,17 @@
+package strategyPattern;
+
+import strategyPattern.enums.OrderType;
+import strategyPattern.factory.OrderStrategyFactory;
+import strategyPattern.service.OrderStrategy;
+
+public class main {
+    public static void main(String[] args) throws Exception {
+        OrderStrategy strategy = OrderStrategyFactory.getStrategy(OrderType.GROUPBY.getType());
+        strategy.placeOrder("1", "2");
+        strategy = OrderStrategyFactory.getStrategy(OrderType.NORMAL.getType());
+        strategy.placeOrder("3", "4");
+        strategy = OrderStrategyFactory.getStrategy(OrderType.SECKILL.getType());
+        strategy.placeOrder("5", "6");
+
+    }
+}

+ 7 - 0
src/main/java/strategyPattern/service/OrderStrategy.java

@@ -0,0 +1,7 @@
+package strategyPattern.service;
+
+//订单接口
+public interface OrderStrategy {
+    //下单
+    public void placeOrder(String userId, String productId);
+}

+ 13 - 0
src/main/java/strategyPattern/service/impl/GroupBuyOrderStrategy.java

@@ -0,0 +1,13 @@
+package strategyPattern.service.impl;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import strategyPattern.service.OrderStrategy;
+public class GroupBuyOrderStrategy   implements OrderStrategy {
+    private static final Logger log = LoggerFactory.getLogger(GroupBuyOrderStrategy.class);
+
+    @Override
+    public void placeOrder(String userId, String productId) {
+        log.info("团购下单成功,userId:{},productId:{}", userId, productId);
+    }
+}

+ 15 - 0
src/main/java/strategyPattern/service/impl/NormalOrderStrategy.java

@@ -0,0 +1,15 @@
+package strategyPattern.service.impl;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import strategyPattern.service.OrderStrategy;
+
+
+
+public class NormalOrderStrategy implements OrderStrategy {
+    private static final Logger log = LoggerFactory.getLogger(NormalOrderStrategy.class);
+    @Override
+    public void placeOrder(String userId, String productId) {
+        log.info("普通下单成功,userId:{},productId:{}", userId, productId);
+    }
+}

+ 14 - 0
src/main/java/strategyPattern/service/impl/SeckillOrderStrategy.java

@@ -0,0 +1,14 @@
+package strategyPattern.service.impl;
+
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import strategyPattern.service.OrderStrategy;
+
+public class SeckillOrderStrategy  implements OrderStrategy {
+    private static final Logger log = LoggerFactory.getLogger(NormalOrderStrategy.class);
+    @Override
+    public void placeOrder(String userId, String productId) {
+        log.info("秒杀下单成功,userId:{},productId:{}", userId, productId);
+    }
+}