diff --git a/mesnac-modules/mesnac-prodmgr/src/main/java/com/mesnac/prodmgr/split/AbstractSplitStrategy.java b/mesnac-modules/mesnac-prodmgr/src/main/java/com/mesnac/prodmgr/split/AbstractSplitStrategy.java new file mode 100644 index 0000000..7d38eaa --- /dev/null +++ b/mesnac-modules/mesnac-prodmgr/src/main/java/com/mesnac/prodmgr/split/AbstractSplitStrategy.java @@ -0,0 +1,20 @@ +package com.mesnac.prodmgr.split; + +import java.util.List; + +// 1. 抽象拆分策略类,定义公共方法和抽象方法 +public abstract class AbstractSplitStrategy { + + // 公共方法:用于输出拆分开始的日志或其他公共行为 + public void logStart(String orderCode) { + System.out.println("开始拆分订单: " + orderCode); + } + + // 公共方法:用于输出拆分完成的日志 + public void logEnd(String orderCode, List workOrders) { + System.out.println("订单 " + orderCode + " 已拆分为 " + workOrders.size() + " 个工单"); + } + + // 抽象方法,具体拆分逻辑由子类实现 + public abstract List splitOrder(String order); +} \ No newline at end of file diff --git a/mesnac-modules/mesnac-prodmgr/src/main/java/com/mesnac/prodmgr/split/OrderSplitter.java b/mesnac-modules/mesnac-prodmgr/src/main/java/com/mesnac/prodmgr/split/OrderSplitter.java new file mode 100644 index 0000000..f690fad --- /dev/null +++ b/mesnac-modules/mesnac-prodmgr/src/main/java/com/mesnac/prodmgr/split/OrderSplitter.java @@ -0,0 +1,21 @@ +package com.mesnac.prodmgr.split; + +import java.util.List; + +// 5. 上下文类,基于不同的策略进行订单拆分 +public class OrderSplitter { + private AbstractSplitStrategy strategy; + + // 设置拆分策略 + public void setStrategy(AbstractSplitStrategy strategy) { + this.strategy = strategy; + } + + // 根据当前策略进行订单拆分 + public List split(String order) { + if (strategy == null) { + throw new IllegalStateException("未设置拆分订单策略!"); + } + return strategy.splitOrder(order); + } +} \ No newline at end of file diff --git a/mesnac-modules/mesnac-prodmgr/src/main/java/com/mesnac/prodmgr/split/SplitByMachineStrategy.java b/mesnac-modules/mesnac-prodmgr/src/main/java/com/mesnac/prodmgr/split/SplitByMachineStrategy.java new file mode 100644 index 0000000..f787183 --- /dev/null +++ b/mesnac-modules/mesnac-prodmgr/src/main/java/com/mesnac/prodmgr/split/SplitByMachineStrategy.java @@ -0,0 +1,22 @@ +package com.mesnac.prodmgr.split; + +import java.util.ArrayList; +import java.util.List; + +// 2. 拆分到机台策略 +public class SplitByMachineStrategy extends AbstractSplitStrategy { + + @Override + public List splitOrder(String order) { + logStart(order); // 调用抽象类的公共方法 + System.out.println("根据机台进行订单拆分..."); + + List workOrders = new ArrayList<>(); + // 假设拆分逻辑 + workOrders.add(order + "_机台1"); + workOrders.add(order + "_机台2"); + + logEnd(order, workOrders); // 调用抽象类的公共方法 + return workOrders; + } +} \ No newline at end of file diff --git a/mesnac-modules/mesnac-prodmgr/src/main/java/com/mesnac/prodmgr/split/SplitByProcessRouteStrategy.java b/mesnac-modules/mesnac-prodmgr/src/main/java/com/mesnac/prodmgr/split/SplitByProcessRouteStrategy.java new file mode 100644 index 0000000..9e10872 --- /dev/null +++ b/mesnac-modules/mesnac-prodmgr/src/main/java/com/mesnac/prodmgr/split/SplitByProcessRouteStrategy.java @@ -0,0 +1,22 @@ +package com.mesnac.prodmgr.split; + +import java.util.ArrayList; +import java.util.List; + +// 4. 根据工艺路线自动拆分策略 +public class SplitByProcessRouteStrategy extends AbstractSplitStrategy { + + @Override + public List splitOrder(String order) { + logStart(order); + System.out.println("根据工艺路线自动拆分订单..."); + + List workOrders = new ArrayList<>(); + // 假设拆分逻辑 + workOrders.add(order + "_工艺步骤1"); + workOrders.add(order + "_工艺步骤2"); + + logEnd(order, workOrders); + return workOrders; + } +} \ No newline at end of file diff --git a/mesnac-modules/mesnac-prodmgr/src/main/java/com/mesnac/prodmgr/split/SplitByStaffStrategy.java b/mesnac-modules/mesnac-prodmgr/src/main/java/com/mesnac/prodmgr/split/SplitByStaffStrategy.java new file mode 100644 index 0000000..12e7c3a --- /dev/null +++ b/mesnac-modules/mesnac-prodmgr/src/main/java/com/mesnac/prodmgr/split/SplitByStaffStrategy.java @@ -0,0 +1,22 @@ +package com.mesnac.prodmgr.split; + +import java.util.ArrayList; +import java.util.List; + +// 3. 拆分到人员策略 +public class SplitByStaffStrategy extends AbstractSplitStrategy { + + @Override + public List splitOrder(String order) { + logStart(order); + System.out.println("根据人员进行订单拆分..."); + + List workOrders = new ArrayList<>(); + // 假设拆分逻辑 + workOrders.add(order + "_员工A"); + workOrders.add(order + "_员工B"); + + logEnd(order, workOrders); + return workOrders; + } +} \ No newline at end of file diff --git a/mesnac-modules/mesnac-prodmgr/src/main/java/com/mesnac/prodmgr/split/StrategyPatternWithAbstractClassDemo.java b/mesnac-modules/mesnac-prodmgr/src/main/java/com/mesnac/prodmgr/split/StrategyPatternWithAbstractClassDemo.java new file mode 100644 index 0000000..2214d11 --- /dev/null +++ b/mesnac-modules/mesnac-prodmgr/src/main/java/com/mesnac/prodmgr/split/StrategyPatternWithAbstractClassDemo.java @@ -0,0 +1,27 @@ +package com.mesnac.prodmgr.split; + +import java.util.List; + + +public class StrategyPatternWithAbstractClassDemo { + + public static void main(String[] args) { + OrderSplitter orderSplitter = new OrderSplitter(); + + String order = "订单12345"; + // 拆分到机台 + orderSplitter.setStrategy(new SplitByMachineStrategy()); + List machineWorkOrders = orderSplitter.split(order); + System.out.println("机台拆分结果: " + machineWorkOrders); + + // 拆分到人员 + orderSplitter.setStrategy(new SplitByStaffStrategy()); + List staffWorkOrders = orderSplitter.split(order); + System.out.println("人员拆分结果: " + staffWorkOrders); + + // 根据工艺路线自动拆分 + orderSplitter.setStrategy(new SplitByProcessRouteStrategy()); + List routeWorkOrders = orderSplitter.split(order); + System.out.println("工艺路线拆分结果: " + routeWorkOrders); + } +}