Bladeren bron

责任链模式检查订单

code_hero 2 weken geleden
bovenliggende
commit
807a3b5ca4

+ 37 - 0
src/main/java/responsibilityPattern/entity/Order.java

@@ -0,0 +1,37 @@
+package responsibilityPattern.entity;
+
+public class Order {
+    String id;
+    Double price;
+    Integer stock;
+    public Order() {}
+    public Order(String id, Double price, Integer stock) {
+        this.id = id;
+        this.price = price;
+        this.stock = stock;
+    }
+
+    public String getId() {
+        return id;
+    }
+
+    public void setId(String id) {
+        this.id = id;
+    }
+
+    public Double getPrice() {
+        return price;
+    }
+
+    public void setPrice(Double price) {
+        this.price = price;
+    }
+
+    public Integer getStock() {
+        return stock;
+    }
+
+    public void setStock(Integer stock) {
+        this.stock = stock;
+    }
+}

+ 18 - 0
src/main/java/responsibilityPattern/handler/OrderHandler.java

@@ -0,0 +1,18 @@
+package responsibilityPattern.handler;
+
+import responsibilityPattern.entity.Order;
+
+public abstract class OrderHandler {
+    private OrderHandler nextHandler;
+    public OrderHandler setNextHandler(OrderHandler nextHandler) {
+        this.nextHandler = nextHandler;
+        return nextHandler;
+    }
+    public void handle(Order order) throws Exception {
+        if(proceed(order)&&nextHandler != null) {
+            nextHandler.handle(order);
+        }
+    }
+
+    abstract boolean proceed(Order order) throws Exception;
+}

+ 21 - 0
src/main/java/responsibilityPattern/handler/OrderPriceHandler.java

@@ -0,0 +1,21 @@
+package responsibilityPattern.handler;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import responsibilityPattern.entity.Order;
+
+import java.util.logging.Handler;
+
+public class OrderPriceHandler extends OrderHandler {
+    private static final Logger log = LoggerFactory.getLogger(OrderPriceHandler.class);
+
+    @Override
+    boolean proceed(Order order) throws Exception {
+        //检查价格
+        if(order.getPrice()>1000){
+            throw new Exception("价格太高");
+        }
+        log.info("下单成功:{}",order);
+        return true;
+    }
+}

+ 14 - 0
src/main/java/responsibilityPattern/handler/OrderStockHandler.java

@@ -0,0 +1,14 @@
+package responsibilityPattern.handler;
+
+import responsibilityPattern.entity.Order;
+
+public class OrderStockHandler extends OrderHandler {
+    @Override
+    boolean proceed(Order order) throws Exception {
+        //检查库存
+        if(order.getStock()<1){
+            throw  new Exception("库存不足");
+        }
+        return true;
+    }
+}

+ 21 - 0
src/main/java/responsibilityPattern/main.java

@@ -0,0 +1,21 @@
+package responsibilityPattern;
+
+import responsibilityPattern.entity.Order;
+import responsibilityPattern.handler.OrderHandler;
+import responsibilityPattern.handler.OrderPriceHandler;
+import responsibilityPattern.handler.OrderStockHandler;
+
+public class main {
+    public static void main(String[] args) {
+        OrderPriceHandler orderPriceHandler = new OrderPriceHandler();
+        OrderStockHandler orderStockHandler = new OrderStockHandler();
+        Order order = new Order("1", 10000D, 0);
+        //先检查库存,在检查价格
+        orderStockHandler.setNextHandler(orderPriceHandler);
+        try {
+            orderStockHandler.handle(order);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+}