Changes 订单拆分示例

dev
yinq 1 month ago
parent ea79b08b0e
commit f491b41607

@ -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<String> workOrders) {
System.out.println("订单 " + orderCode + " 已拆分为 " + workOrders.size() + " 个工单");
}
// 抽象方法,具体拆分逻辑由子类实现
public abstract List<String> splitOrder(String order);
}

@ -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<String> split(String order) {
if (strategy == null) {
throw new IllegalStateException("未设置拆分订单策略!");
}
return strategy.splitOrder(order);
}
}

@ -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<String> splitOrder(String order) {
logStart(order); // 调用抽象类的公共方法
System.out.println("根据机台进行订单拆分...");
List<String> workOrders = new ArrayList<>();
// 假设拆分逻辑
workOrders.add(order + "_机台1");
workOrders.add(order + "_机台2");
logEnd(order, workOrders); // 调用抽象类的公共方法
return workOrders;
}
}

@ -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<String> splitOrder(String order) {
logStart(order);
System.out.println("根据工艺路线自动拆分订单...");
List<String> workOrders = new ArrayList<>();
// 假设拆分逻辑
workOrders.add(order + "_工艺步骤1");
workOrders.add(order + "_工艺步骤2");
logEnd(order, workOrders);
return workOrders;
}
}

@ -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<String> splitOrder(String order) {
logStart(order);
System.out.println("根据人员进行订单拆分...");
List<String> workOrders = new ArrayList<>();
// 假设拆分逻辑
workOrders.add(order + "_员工A");
workOrders.add(order + "_员工B");
logEnd(order, workOrders);
return workOrders;
}
}

@ -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<String> machineWorkOrders = orderSplitter.split(order);
System.out.println("机台拆分结果: " + machineWorkOrders);
// 拆分到人员
orderSplitter.setStrategy(new SplitByStaffStrategy());
List<String> staffWorkOrders = orderSplitter.split(order);
System.out.println("人员拆分结果: " + staffWorkOrders);
// 根据工艺路线自动拆分
orderSplitter.setStrategy(new SplitByProcessRouteStrategy());
List<String> routeWorkOrders = orderSplitter.split(order);
System.out.println("工艺路线拆分结果: " + routeWorkOrders);
}
}
Loading…
Cancel
Save