zhenqingshi
2020-04-24 16:32
采纳率: 33.3%
浏览 226
已采纳

超多判断的if和else需要改成设计模式或者进行其他优化吗?

public static String getModelLevel(String modelNum, String modelName) {
        String level = "";
        if (modelNum.startsWith("2")) {
            // 固定板--完全匹配
            if ("固定板".equals(modelName) || modelName.contains("支撑板") || modelName.contains("轴") || modelName.contains("辊")
                    || modelName.contains("切刀") || modelName.contains("压刀") || modelName.contains("模具")
                    || modelName.contains("模架") || modelName.contains("治具") || modelName.contains("方通")
                    || modelName.contains("机架") || modelName.contains("支架") || modelName.contains("铝型材")
                    || modelName.contains("底座") || modelName.contains("安装座") || modelName.contains("齿条")
                    || modelName.contains("滚轮") || modelName.contains("齿轮") || modelName.contains("销")
                    || modelName.contains("键") || modelName.contains("转塔") || modelName.contains("框架")
                    || modelName.contains("大理石") || modelName.contains("滑台") || modelName.contains("垫圈")
                    || modelName.contains("单轴") || modelName.contains("气缸") || modelName.contains("电机")
                    || modelName.contains("大板") || modelName.contains("底板") || modelName.contains("支撑板")
                    || modelName.contains("连接板") || modelName.contains("立板") || modelName.contains("侧板")
                    || modelName.contains("安装板") || modelName.contains("电木板") || modelName.contains("动板")) {
                level = "A";
            } else if (modelName.contains("板") || modelName.contains("支撑") || modelName.contains("轴承")
                    || modelName.contains("加强筋") || modelName.contains("滑轨") || modelName.contains("导轨")
                    || modelName.contains("滑座") || modelName.contains("滑道") || modelName.contains("线轨")
                    || modelName.contains("丝杆") || modelName.contains("轴承") || modelName.contains("压")
                    || modelName.contains("导向") || modelName.contains("调节") || modelName.contains("同步")
                    || modelName.contains("支撑") || modelName.contains("座") || modelName.contains("轮")
                    || modelName.contains("杆") || modelName.contains("柱") || modelName.contains("块")
                    || modelName.contains("片") || modelName.contains("梁") || modelName.contains("圈")
                    || modelName.contains("环") || modelName.contains("套") || modelName.contains("棒")
                    || modelName.contains("台") || modelName.contains("安装块") || modelName.contains("安装码")
                    || modelName.contains("支撑架") || modelName.contains("基米") || modelName.contains("法兰")
                    || modelName.contains("组件") || modelName.contains("转盘") || modelName.contains("浮动接头")
                    || modelName.contains("弹簧") || modelName.contains("垫板") || modelName.contains("螺钉")
                    || modelName.contains("不常用螺丝") || modelName.contains("螺母")) {
                level = "B";
            } else if (modelName.contains("吸盘") || modelName.contains("爪") || modelName.contains("漏斗")
                    || modelName.contains("缓冲") || modelName.contains("扫码") || modelName.contains("感应")
                    || modelName.contains("石") || modelName.contains("钩") || modelName.contains("勾")
                    || modelName.contains("尺") || modelName.contains("箱") || modelName.contains("件")
                    || modelName.contains("皮带") || modelName.contains("头") || modelName.contains("架")
                    || modelName.contains("挡条") || modelName.contains("替罪羊") || modelName.contains("夹子")
                    || modelName.contains("胶") || modelName.contains("拖链") || modelName.contains("铭牌")
                    || modelName.contains("毛刷") || modelName.contains("吸板") || modelName.contains("盖板")) {
                level = "C";
            } else if (modelName.contains("针") || modelName.contains("门") || modelName.contains("钣金")
                    || modelName.contains("管道") || modelName.contains("爬梯") || modelName.contains("盒")
                    || modelName.contains("桶") || modelName.contains("海绵") || modelName.contains("把手")
                    || modelName.contains("槽") || modelName.contains("仓") || modelName.contains("护罩")
                    || modelName.contains("玻璃") || modelName.contains("管") || modelName.contains("通道")
                    || modelName.contains("罩") || modelName.contains("冷轧板") || modelName.contains("踏板")
                    || modelName.contains("亚克力封板") || modelName.contains("封板") || modelName.contains("指针")) {
                level = "D";
            }
        } else if (modelNum.startsWith("3")) {
            if (modelName.contains("台阶销") || modelName.contains("圆柱销") || modelName.contains("齿轮")
                    || modelName.contains("键") || modelName.contains("垫圈")) {
                level = "A";
            } else if (modelName.contains("加强筋") || modelName.contains("轴承") || modelName.contains("止回座块")
                    || modelName.contains("止回卡爪") || modelName.contains("支架") || modelName.contains("支柱")
                    || modelName.contains("调节") || modelName.contains("浮动接头") || modelName.contains("直线导轨")
                    || modelName.contains("调节螺杆") || modelName.contains("回程导轨") || modelName.contains("板")
                    || modelName.contains("限位")) {
                level = "B";
            } else if (modelName.contains("吸盘固定座") || modelName.contains("旋钮") || modelName.contains("缓冲器限位套")
                    || modelName.contains("光电固定块") || modelName.contains("传感器防护套") || modelName.contains("设备铭牌")
                    || modelName.contains("线槽防护件") || modelName.contains("扎线固定座") || modelName.contains("工位铭牌")
                    || modelName.contains("盖板") || modelName.contains("门吸") || modelName.contains("显示器安装块")
                    || modelName.contains("罩") || modelName.contains("拖链分隔块") || modelName.contains("拉手")
                    || modelName.contains("盒")) {
                level = "C";
            }
        } else if (modelNum.startsWith("4")) {
            if (modelNum.startsWith("40101") || modelNum.startsWith("40206") || modelNum.startsWith("40301")
                    || modelNum.startsWith("40303") || modelNum.startsWith("40304") || modelNum.startsWith("40405")
                    || modelNum.startsWith("40409") || modelNum.startsWith("40108")) {
                level = "A";
            } else if (modelNum.startsWith("40403") || modelNum.startsWith("40305") || modelNum.startsWith("40302")
                    || modelNum.startsWith("40203") || modelNum.startsWith("40207") || modelNum.startsWith("40109")
                    || modelNum.startsWith("40202") || modelNum.startsWith("40102")) {
                level = "B";
            } else if (modelNum.startsWith("40104") || modelNum.startsWith("40105") || modelNum.startsWith("40106")
                    || modelNum.startsWith("40204") || modelNum.startsWith("40205") || modelNum.startsWith("40208")
                    || modelNum.startsWith("40306") || modelNum.startsWith("40201")) {
                level = "C";
            } else if (modelNum.startsWith("40103") || modelNum.startsWith("40110") || modelNum.startsWith("40501")
                    || modelNum.startsWith("40502") || modelNum.startsWith("40401") || modelNum.startsWith("40404")
                    || modelNum.startsWith("40601") || modelNum.startsWith("40602") || modelNum.startsWith("40603")
                    || modelNum.startsWith("40604") || modelNum.startsWith("4080") || modelNum.startsWith("40407")) {
                level = "D";
            }
        }
        return level;
    }
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • Json-Huang 2020-04-24 22:25
    已采纳

    1.“支撑板”这些项建议换成枚举存取并做好分类,或者存取到配置表中,通过in查询sql方式进行判断;
    2.面向对象而不是面向方法编程,考虑使用多态抽象类
    3.if可以结合设计模式,比如抽象工厂模式;

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • mollpppp 2020-04-24 20:18

    代码结构太乱,我推荐在把判断条件分出来,搞个函数来返回判别。

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题