java 卫语句_卫语句

阿里巴巴Java开发手册——“多层条件语句建议使用卫语句、策略模式、状态模式等方式重构”。

卫语句

把复杂的条件表达式拆分成多个条件表达式

下面是部分特殊车牌的校验代码

public static String getLicensePlateColor(String licensePlate) {

/**

* 默认车牌颜色为蓝色

*/

String color = PmDicConstant.DIC_KEY_PM_LICENSE_PLATE_COLOR_BLUE;

//首先判断普通车辆

Matcher matcher = pattern.matcher(licensePlate);

if (!matcher.find()) {

//新能源车

if (licensePlate.trim().length() == 8) {

color = PmDicConstant.DIC_KEY_PM_LICENSE_PLATE_COLOR_GREEN;

//新能源中警车和教练车

if (licensePlate.trim().endsWith("学") || licensePlate.trim().endsWith("警")) {

color = PmDicConstant.DIC_KEY_PM_LICENSE_PLATE_COLOR_WHITE;

}

} else if (licensePlate.trim().endsWith("警")) {

//普通警车

color = PmDicConstant.DIC_KEY_PM_LICENSE_PLATE_COLOR_WHITE;

} else if (licensePlate.trim().endsWith("学")) {

//普通教练车

color = PmDicConstant.DIC_KEY_PM_LICENSE_PLATE_COLOR_YELLOW;

}

}

return color;

}

使用卫语句优化代码:

public static String getLicensePlateColor(String licensePlate) {

/**

* 默认车牌颜色为蓝色

*/

String color = PmDicConstant.DIC_KEY_PM_LICENSE_PLATE_COLOR_BLUE;

//首先判断普通车辆

Matcher matcher = pattern.matcher(licensePlate);

if (!matcher.find()) {

//新能源车

if (licensePlate.trim().length() == 8) {

color = PmDicConstant.DIC_KEY_PM_LICENSE_PLATE_COLOR_GREEN;

//新能源中警车和教练车

if (licensePlate.trim().endsWith("学") || licensePlate.trim().endsWith("警")) {

color = PmDicConstant.DIC_KEY_PM_LICENSE_PLATE_COLOR_WHITE;

return color;

}

return color;

}

if (licensePlate.trim().endsWith("警")) {

//普通警车

color = PmDicConstant.DIC_KEY_PM_LICENSE_PLATE_COLOR_WHITE;

return color;

}

if (licensePlate.trim().endsWith("学")) {

//普通教练车

color = PmDicConstant.DIC_KEY_PM_LICENSE_PLATE_COLOR_YELLOW;

return color;

}

}

return color;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值