如何处理多条件订单状态的变化方式?

在一个点餐系统中,有着外卖单,堂食单等。对应不同的订单与不同的动作和设置会有不同的状态变化,在代码中if elseif要写30-40多行,对于这种情况是否有更好的处理方法?

举例如下:
判断项:商家开启先食后付 是否自动接单 是否有打印机 打印是否成功 是否自动清台
动作: 下单 接单 拒单 顾客取消单 顾客支付 清台 结账
状态: 待确认 待处理 拒绝 完成 取消 进行中 超时 申请退款

如上由5种判断项 组合出多种变化 在每种变化中 不同的动作 改变订单不同的状态,像这种状态模型,除了无数的 if elseif 是否有更好的方法实现?

想要实现:输入动作 输出状态

想要一个思路,非常感谢!


Smart猫小萌
浏览 790回答 4
4回答

千巷猫影

给一个大概思路吧。java实现。可以稍微参考下。package com.ui;import java.util.ArrayList;import java.util.Arrays;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;public class OrderStatusService {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* 映射关系列表&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public&nbsp; static List<OrderStatusMapper> mapperList = new ArrayList<OrderStatusMapper>();&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; static {&nbsp; &nbsp; &nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* 初始化判断项、订单动作到状态的映射,这里只为演示,实际中映射关系最好保存在配置文件中或者数据库中,一般在应用启动的时候,从配置文件或者数据库加载,保存一份即可。&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; OrderStatusMapper m1 = new OrderStatusMapper();&nbsp; &nbsp; &nbsp; &nbsp; m1.addJudgementItems(1,2,4,6) //先吃后付 and 自动接单 and 有打印机 and 打印成功&nbsp; &nbsp; &nbsp; &nbsp; .addAction2statusItem(1,6)//下单动作:进行中&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; .addAction2statusItem(7, 4); //结账动作:已完成&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; OrderStatusMapper m2 = new OrderStatusMapper();&nbsp; &nbsp; &nbsp; &nbsp; m2.addJudgementItems(3,5)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; .addAction2statusItem(2,1)&nbsp; &nbsp; &nbsp; &nbsp; .addAction2statusItem(4, 3);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; mapperList.add(m1);&nbsp; &nbsp; &nbsp; &nbsp; mapperList.add(m2);&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* 是否是相等的set&nbsp; &nbsp; &nbsp;* @param source&nbsp;&nbsp; &nbsp; &nbsp;* @param dest&nbsp; &nbsp; &nbsp;* @return&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public boolean&nbsp; isEqualSet(Set<Integer> source,Set<Integer> dest){&nbsp; &nbsp; &nbsp; &nbsp; if(null == source || null == dest){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if(source.size() != dest.size()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; for(Iterator<Integer> it = source.iterator();it.hasNext();){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(!dest.contains(it.next())){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; Order order = new Order();&nbsp; &nbsp; &nbsp; &nbsp; order.setOrderAction(1);//下单&nbsp; &nbsp; &nbsp; &nbsp; order.setJudgementItems(new HashSet<Integer>(Arrays.asList(1,2,4,6)));//先吃后付 and 自动接单 and 有打印机 and 打印成功&nbsp; &nbsp; &nbsp; &nbsp; //获取订单下一个状态&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(new OrderStatusService().getNextOrderStatus(order));//&nbsp; 结果为 6(进行中)&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* 获取下一个订单状态&nbsp; &nbsp; &nbsp;* @param curOrder&nbsp; &nbsp; &nbsp;* @return&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public Integer getNextOrderStatus(Order curOrder){&nbsp; &nbsp; &nbsp; &nbsp; for(OrderStatusMapper mapper : mapperList){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(isEqualSet(curOrder.getJudgementItems(), mapper.getJudgementItems()) &&&nbsp; null != curOrder.getOrderAction()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return mapper.getAction2statusMap().get(curOrder.getOrderAction());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;}/**&nbsp;* 订单类型,订单动作到状态的映射。&nbsp;*&nbsp;*/class OrderStatusMapper{&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* 判断项&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private Set<Integer> judgementItems = new HashSet<Integer>();&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* 订单动作到状态关系&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private Map<Integer,Integer> action2statusMap = new HashMap<Integer,Integer>();&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* 下一个订单状态&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private Integer nextStatus;&nbsp; &nbsp; public Set<Integer> getJudgementItems() {&nbsp; &nbsp; &nbsp; &nbsp; return judgementItems;&nbsp; &nbsp; }&nbsp; &nbsp; public OrderStatusMapper addJudgementItems(Integer ...items) {&nbsp; &nbsp; &nbsp; &nbsp; if(null == items || items.length <= 0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return this;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; for(Integer cur : items){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.judgementItems.add(cur);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return this;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; public Map<Integer, Integer> getAction2statusMap() {&nbsp; &nbsp; &nbsp; &nbsp; return action2statusMap;&nbsp; &nbsp; }&nbsp; &nbsp; public OrderStatusMapper addAction2statusItem(Integer action,Integer status) {&nbsp; &nbsp; &nbsp; &nbsp; if(null == action || null == status){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return this;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; this.action2statusMap.put(action, status);&nbsp; &nbsp; &nbsp; &nbsp; return this;&nbsp; &nbsp; }&nbsp; &nbsp; public Integer getNextStatus() {&nbsp; &nbsp; &nbsp; &nbsp; return nextStatus;&nbsp; &nbsp; }&nbsp; &nbsp; public OrderStatusMapper setNextStatus(Integer nextStatus) {&nbsp; &nbsp; &nbsp; &nbsp; this.nextStatus = nextStatus;&nbsp; &nbsp; &nbsp; &nbsp; return this;&nbsp; &nbsp; }}class Order{&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* 判断项目&nbsp; &nbsp; &nbsp;* 1:商家开启先食后付 ;&nbsp; &nbsp; &nbsp;* 2:自动接单;&nbsp; &nbsp; &nbsp;* 3:不自动接单&nbsp; &nbsp; &nbsp;* 4:有打印机&nbsp; &nbsp; &nbsp;* 5:没有打印机&nbsp; &nbsp; &nbsp;* 6:打印成功&nbsp; &nbsp; &nbsp;* 7:打印不成功&nbsp; &nbsp; &nbsp;* 8:自动清台&nbsp; &nbsp; &nbsp;* 9:不自动清台&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private Set<Integer> judgementItems;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* 订单动作&nbsp; &nbsp; &nbsp;* 1:下单&nbsp;&nbsp; &nbsp; &nbsp;* 2:接单&nbsp;&nbsp; &nbsp; &nbsp;* 3:拒单&nbsp;&nbsp; &nbsp; &nbsp;* 4:顾客取消单&nbsp; &nbsp; &nbsp;* 5:顾客支付&nbsp;&nbsp; &nbsp; &nbsp;* 6:清台&nbsp; &nbsp; &nbsp;* 7:结账&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private Integer orderAction;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* 订单状态&nbsp; &nbsp; &nbsp;* 1:待确认&nbsp;&nbsp; &nbsp; &nbsp;* 2:待处理&nbsp;&nbsp; &nbsp; &nbsp;* 3:拒绝&nbsp;&nbsp; &nbsp; &nbsp;* 4:完成&nbsp;&nbsp; &nbsp; &nbsp;* 5:取消&nbsp;&nbsp; &nbsp; &nbsp;* 6:进行中&nbsp;&nbsp; &nbsp; &nbsp;* 7:超时&nbsp;&nbsp; &nbsp; &nbsp;* 8:申请退款&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private Integer orderStatus;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; public Set<Integer> getJudgementItems() {&nbsp; &nbsp; &nbsp; &nbsp; return judgementItems;&nbsp; &nbsp; }&nbsp; &nbsp; public void setJudgementItems(Set<Integer> judgementItems) {&nbsp; &nbsp; &nbsp; &nbsp; this.judgementItems = judgementItems;&nbsp; &nbsp; }&nbsp; &nbsp; public Integer getOrderAction() {&nbsp; &nbsp; &nbsp; &nbsp; return orderAction;&nbsp; &nbsp; }&nbsp; &nbsp; public void setOrderAction(Integer orderAction) {&nbsp; &nbsp; &nbsp; &nbsp; this.orderAction = orderAction;&nbsp; &nbsp; }&nbsp; &nbsp; public Integer getOrderStatus() {&nbsp; &nbsp; &nbsp; &nbsp; return orderStatus;&nbsp; &nbsp; }&nbsp; &nbsp; public void setOrderStatus(Integer orderStatus) {&nbsp; &nbsp; &nbsp; &nbsp; this.orderStatus = orderStatus;&nbsp; &nbsp; }}&nbsp;

猛跑小猪

试试看位运算可不可以, 每个比特位表示一个条件的状态.比如当前状态为: 0111某个动作的执行条件为: 0100 即只要第二个状态位为真的时候执行运算的时候就判断 0111 & 0100 == 0100 就可以判断这个动作要不要执行了这样每个动作对应一个 if, 会比原来少一点, 但是状态转换的代码可能会多一点, 这个就看楼主权衡了

萧十郎

状态切换和流程控制可以使用有限状态机定义与处理,相关开源的库也比较丰富。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript