Java枚举类在生产环境中的使用方式
前言
Java枚举在项目中使用非常普遍,许多人在做项目时,一定会遇到要维护某些业务场景状态的时候,往往会定义一个常量类,然后添加业务场景相关的状态常量。但实际上,生产环境的项目中业务状态的定义大部分是由枚举类来完成的,因为更加清晰明确,还能自定义不同的方法来获取对应的业务状态值,十分方便。
以下代码均为生产环境已上线项目的代码片段,仅供参考。
使用
大体分为确定业务场景状态、定义枚举类、自定义查询方法、测试效果等几个部分。
1、确定业务场景状态
以我工作中实际的项目为例,智慧医院在挂号、门诊缴费时需要使用支付功能,我们目前实现了以下几种支付形式:微信小程序支付、微信H5支付、支付宝小程序支付、支付宝生活号支付、微信医保支付。
那么,我们就可以针对这几种支付形式定义一个枚举类专门维护,今后需要新增、修改以及删除时,只需要修改这个枚举类即可。
2、定义枚举类
public enum PayTypeEnum {
WEI_XIN_MINI_APP("1", "wxma", "微信小程序支付"),
WEI_XIN_H5("2", "wxh5", "微信H5支付"),
ZFB_MINI_APP("3", "zfbma", "支付宝小程序支付"),
ZFB_H5("4", "zfbh5", "支付宝生活号支付"),
WEI_XIN_MEDICAL("5", "wxmedical", "微信医保支付");
private final String id;
private final String code;
private final String label;
PayTypeEnum(final String id, final String code, final String label) {
域名 = id;
域名 = code;
域名l = label;
}
public String getId() {
return id;
}
public String getCode() {
return code;
}
public String getLabel() {
return label;
}
}
3、自定义查询方法
枚举类我们定义了id、code、label,那么我们使用过程中可能需要根据id获取枚举值、根据code获取枚举值(本人大部分时候都定义的这两个),甚至根据label获取枚举值,因此可以根据需要自定义自己的查询方法。
/**
* 根据id获取枚举对象
* @param id
*/
public static PayTypeEnum findById(String id) {
for (PayTypeEnum type : 域名es()) {
if (域名d().equals(id))
return type;
}
return null;
}
/**
* 根据code获取枚举对象
* @param code
*/
public static PayTypeEnum findByCode(String code) {
for (PayTypeEnum type : 域名es()) {
if (域名ode().equals(code))
return type;
}
return null;
}
为了更完善,我们还可以再定义一个检查枚举类型的方法。
/**
* 检查支付类型是否有效
* @param id
*/
public static void check(String id) {
if (域名pty(id)) {
throw new BadRequestAlertException("无效的支付类型", "PayTypeEnum", "无效的支付类型");
}
for (PayTypeEnum type : 域名es()) {
if (域名d().equals(id)) {
return;
}
}
throw new BadRequestAlertException("无效的支付类型", "PayTypeEnum", "无效的支付类型");
}
最终代码如下:
import 域名.域名equestAlertException;
import 域名.StringUtils;
public enum PayTypeEnum {
WEI_XIN_MINI_APP("1", "wxma", "微信小程序支付"),
WEI_XIN_H5("2", "wxh5", "微信H5支付"),
ZFB_MINI_APP("3", "zfbma", "支付宝小程序支付"),
ZFB_H5("4", "zfbh5", "支付宝生活号支付"),
WEI_XIN_MEDICAL("5", "wxmedical", "微信医保支付");
private final String id;
private final String code;
private final String label;
PayTypeEnum(final String id, final String code, final String label) {
域名 = id;
域名 = code;
域名l = label;
}
public String getId() {
return id;
}
public String getCode() {
return code;
}
public String getLabel() {
return label;
}
/**
* 根据id获取枚举对象
* @param id
*/
public static PayTypeEnum findById(String id) {
for (PayTypeEnum type : 域名es()) {
if (域名d().equals(id))
return type;
}
return null;
}
/**
* 根据code获取枚举对象
* @param code
*/
public static PayTypeEnum findByCode(String code) {
for (PayTypeEnum type : 域名es()) {
if (域名ode().equals(code))
return type;
}
return null;
}
/**
* 检查支付类型是否有效
* @param id
*/
public static void check(String id) {
if (域名pty(id)) {
throw new BadRequestAlertException("无效的支付类型", "PayTypeEnum", "无效的支付类型");
}
for (PayTypeEnum type : 域名es()) {
if (域名d().equals(id)) {
return;
}
}
throw new BadRequestAlertException("无效的支付类型", "PayTypeEnum", "无效的支付类型");
}
}
4、测试效果
public static void main(String[] args) {
域名tln("============= 获取枚举类的值 =============");
域名tln("获取id:" + 域名d());
域名tln("获取code:" + 域名ode());
域名tln("获取label:" + 域名abel());
域名tln("============= 根据自定义的查询方法获取值 =============");
域名tln("根据id获取枚举对象:" + 域名ById("3"));
域名tln("根据code获取枚举对象:" + 域名ByCode("zfbma"));
域名tln("============= 类型有效性检查 =============");
域名t("检查1:");
域名k("1");
域名tln();
域名t("检查2:");
域名k("999");
}
打印如下:
============= 获取枚举类的值 =============
获取id:1
获取code:wxma
获取label:微信小程序支付
============= 根据自定义的查询方法获取值 =============
根据id获取枚举对象:ZFB_MINI_APP
根据code获取枚举对象:ZFB_MINI_APP
============= 类型有效性检查 =============
检查1:
检查2:无效的支付类型
Process finished with exit code 0
总结
Java枚举类的定义,大部分都和业务场景有关,但凡是类似于业务状态值的定义,最好都使用枚举类,这样便于维护和阅读,但每个工程师和研发团队的风格都是不同的,仅以个人这些年的工作经历而言,往往参与一个项目,到后期会形成大量的枚举类,而不是大量的常量类,常量类顶多只有一个,太多的话根本无法维护,尤其是人员变更之后,新来的同事对于大量的常量类感到头疼,但枚举类却能清晰的表达该业务的场景及用法。
如果喜欢的话,麻烦一键……啊不,点个赞 ,觉得有用也可以点个推荐咯~(o..o)