入门的时候,针对某一字段状态的判断,一开始是在前端用if else 判断,有一些弊端:①把内置的code暴露给用户②if else最好不要超过3层③前端很长一段冗余判断不规范后改进使用枚举,在后台进行处理,将代码整理如下:
public enum UserStatus { NORMAL("正常", "00"), SINGLED("单项停机", "01"), STOP("停机", "02"), PRESALE("预销号", "03"), CANCEL("销号", "04"), TRANSFER("过户", "05"), SLEEP("休眠", "06"), TOEXCITED("待激", "07"); private String description; private String code; UserStatus(String name, String index) { this.description = name; this.code = index; } static Map<String, String> map = Maps.newHashMap(); static { for (UserStatus s : UserStatus.values()) { map.put(s.code, s.description); } } public static String parse(String code) { return map.get(code); } }
c.setUserStatus(UserStatus.parse(c.getUserStatus()));