命令实现
实物商品实现类:
/** * 实物订单处理 */ public class InKindOrderDetailCommand implements OrderDetailService { @Override public Map<String, List<OrderDetailDto>> orderDetailProcess(List<OrderDetailDto> orderDetailList) { // 订单中的实物 List<OrderDetailDto> resultList = orderDetailList.stream() // 筛选出实物 .filter(item -> Objects.equals(1, item.getCommodityType())) .peek(item -> { // 计算原价 item.setOriginalPrice(item.getCommodityUnitPrice().multiply(new BigDecimal(item.getCommodityQuantity()))); // 计算其他数据项目 .... }).collect(toList()); return new HashMap<String, List<OrderDetailDto>>() { { put("inKind", resultList); } }; } }
虚拟商品实现类:
public class VirtualItemOrderDetailCommand implements OrderDetailService { @Override public Map<String, List<OrderDetailDto>> orderDetailProcess(List<OrderDetailDto> orderDetailList) { return null; } }
其他商品实现类:
/** * 其他物品 */ public class OtherOrderDetailCommand implements OrderDetailService { @Override public Map<String, List<OrderDetailDto>> orderDetailProcess(List<OrderDetailDto> orderDetailList) { return null; } }
命令调用
调用工具类(Terminial 类)
public class OrderDetailServices { public List<OrderDetailService> orderDetailServiceList = new ArrayList<>(); /** * 初始化方法 */ public static OrderDetailServices create() { OrderDetailServices orderDetailServices = new OrderDetailServices(); // 实物 OrderDetailService inKindOrderDetail = new InKindOrderDetailCommand(); // 虚拟物品 OrderDetailService virtualItemOrderDetail = new VirtualItemOrderDetailCommand(); // 赠送物品(优惠券、积分) OrderDetailService giveAwayOrderDetail = new GiveAwayOrderDetailCommand(); // 其他 OrderDetailService otherOrderDetail = new OtherOrderDetailCommand(); orderDetailServices.orderDetailServiceList = Arrays.asList(inKindOrderDetail, virtualItemOrderDetail, giveAwayOrderDetail, otherOrderDetail); return orderDetailServices; } public Map<String, List<OrderDetailDto>> orderDetailProcess(List<OrderDetailDto> orderDetailList) { Map<String, List<OrderDetailDto>> resultMap = new HashMap<>(); for (OrderDetailService item : orderDetailServiceList) { Map<String, List<OrderDetailDto>> tmpMap = item.orderDetailProcess(orderDetailList); if (tmpMap != null && !tmpMap.isEmpty()) { resultMap.putAll(tmpMap); } } return resultMap; } }
测试类:
public class OrderDetailServiceTest { public static void main(String[] args) { OrderDetailServices detailServices = OrderDetailServices.create(); OrderDetailDto orderDetailDto = new OrderDetailDto(); orderDetailDto.setOrderCode("CD00001"); orderDetailDto.setCommodityType(1); orderDetailDto.setCommodityName("GTX 3080 TI"); orderDetailDto.setCommodityUnitPrice(new BigDecimal("8999")); orderDetailDto.setCommodityQuantity(2); OrderDetailDto orderDetailDto2 = new OrderDetailDto(); orderDetailDto2.setCommodityType(1); orderDetailDto2.setOrderCode("CD00002"); orderDetailDto2.setCommodityName("LEI SHEN 茶轴 1088"); orderDetailDto2.setCommodityUnitPrice(new BigDecimal("488")); orderDetailDto2.setCommodityQuantity(1); OrderDetailDto orderDetailDto3 = new OrderDetailDto(); orderDetailDto3.setCommodityType(2); orderDetailDto3.setOrderCode("CJ00002"); orderDetailDto3.setCommodityName("满 5000 减 200"); orderDetailDto3.setCommodityUnitPrice(new BigDecimal("200")); orderDetailDto3.setCommodityQuantity(2); OrderDetailDto orderDetailDto4 = new OrderDetailDto(); orderDetailDto4.setCommodityType(2); orderDetailDto4.setOrderCode("CJ00002"); orderDetailDto4.setCommodityName("满 5000 减 100"); orderDetailDto4.setCommodityUnitPrice(new BigDecimal("100")); orderDetailDto4.setCommodityQuantity(1); List<OrderDetailDto> detailList = Arrays.asList( orderDetailDto, orderDetailDto2, orderDetailDto3, orderDetailDto4); Map<String, List<OrderDetailDto>> resultMap = detailServices .orderDetailProcess(detailList); System.out.println(resultMap); } }
总结
命令模式非常适合于数据的处理,支持撤销、重做。将命令处理对象以参数的形式进行传递,最后对数据进行一个统一的处理。它的优缺点如下:
优点:降低耦合度、非常容易拓展、比如上面我在增加一个详情分类非常容易。
缺点:如果当命令非常多的情况下,可能会导致项目的 “类爆炸”。