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) {
this.id = id;
this.code = code;
this.label = 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 : PayTypeEnum.values()) {
if (type.getId().equals(id))
return type;
}
return null;
}
/**
* 根据code获取枚举对象
* @param code
*/
public static PayTypeEnum findByCode(String code) {
for (PayTypeEnum type : PayTypeEnum.values()) {
if (type.getCode().equals(code))
return type;
}
return null;
}
为了更完善,我们还可以再定义一个检查枚举类型的方法。
/**
* 检查支付类型是否有效
* @param id
*/
public static void check(String id) {
if (StringUtils.isEmpty(id)) {
throw new BadRequestAlertException("无效的支付类型", "PayTypeEnum", "无效的支付类型");
}
for (PayTypeEnum type : PayTypeEnum.values()) {
if (type.getId().equals(id)) {
return;
}
}
throw new BadRequestAlertException("无效的支付类型", "PayTypeEnum", "无效的支付类型");
}
最终代码如下:
import com.web.rest.errors.BadRequestAlertException;
import org.springframework.util.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) {
this.id = id;
this.code = code;
this.label = 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 : PayTypeEnum.values()) {
if (type.getId().equals(id))
return type;
}
return null;
}
/**
* 根据code获取枚举对象
* @param code
*/
public static PayTypeEnum findByCode(String code) {
for (PayTypeEnum type : PayTypeEnum.values()) {
if (type.getCode().equals(code))
return type;
}
return null;
}
/**
* 检查支付类型是否有效
* @param id
*/
public static void check(String id) {
if (StringUtils.isEmpty(id)) {
throw new BadRequestAlertException("无效的支付类型", "PayTypeEnum", "无效的支付类型");
}
for (PayTypeEnum type : PayTypeEnum.values()) {
if (type.getId().equals(id)) {
return;
}
}
throw new BadRequestAlertException("无效的支付类型", "PayTypeEnum", "无效的支付类型");
}
}
4、测试效果
public static void main(String[] args) {
System.out.println("============= 获取枚举类的值 =============");
System.out.println("获取id:" + PayTypeEnum.WEI_XIN_MINI_APP.getId());
System.out.println("获取code:" + PayTypeEnum.WEI_XIN_MINI_APP.getCode());
System.out.println("获取label:" + PayTypeEnum.WEI_XIN_MINI_APP.getLabel());
System.out.println("============= 根据自定义的查询方法获取值 =============");
System.out.println("根据id获取枚举对象:" + PayTypeEnum.findById("3"));
System.out.println("根据code获取枚举对象:" + PayTypeEnum.findByCode("zfbma"));
System.out.println("============= 类型有效性检查 =============");
System.out.print("检查1:");
PayTypeEnum.check("1");
System.out.println();
System.out.print("检查2:");
PayTypeEnum.check("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)
Java枚举类在生产环境中的使用方式的更多相关文章
- JDK 9 发布仅数月,为何在生产环境中却频遭嫌弃?
千呼万唤始出来,在经历了整整一年的跳票之后,Java 9 终于在 9 月 21 日拨开云雾,露出真正的面目.对众多 Java 程序员来说,这一天无疑是一个重大的日子,首先 Java 开发者们再也不用羡 ...
- 【原】Storm Local模式和生产环境中Topology运行配置
Storm入门教程 1. Storm基础 Storm Storm主要特点 Storm基本概念 Storm调度器 Storm配置 Guaranteeing Message Processing(消息处理 ...
- Kubernetes 在生产环境中常用架构
Kubernetes 在生产环境中常用架构 首先,我们来梳理下Kubernetes生产架构,其设计适用于绝大多数环境.如下图所示 在该架构中,我们可以将其分为四层,如下: Client层:即Kuber ...
- 理解Docker(6):若干企业生产环境中的容器网络方案
本系列文章将介绍 Docker的相关知识: (1)Docker 安装及基本用法 (2)Docker 镜像 (3)Docker 容器的隔离性 - 使用 Linux namespace 隔离容器的运行环境 ...
- 生产环境中使用Docker Swarm的一些建议
译者按: 实践中会发现,生产环境中使用单个Docker节点是远远不够的,搭建Docker集群势在必行.然而,面对Kubernetes, Mesos以及Swarm等众多容器集群系统,我们该如何选择呢?它 ...
- Dubbo Mesh 在闲鱼生产环境中的落地实践
本文作者至简曾在 2018 QCon 上海站以<Service Mesh 的本质.价值和应用探索>为题做了一次分享,其中谈到了 Dubbo Mesh 的整体发展思路是“借力开源.反哺开源” ...
- 明白生产环境中的jvm参数
明白生产环境中的jvm参数 写代码的时候,程序写完了,发到线上去运行,跑一段时间后,程序变慢了,cpu负载高了--一堆问题出来了,所以了解一下生产环境的机器上的jvm配置是有必要的.比如说: JDK版 ...
- 生产环境中tomcat的配置
生产环境中要以daemon方式运行tomcat 通常在开发环境中,我们使用$CATALINA_HOME/bin/startup.sh来启动tomcat, 使用$CATALINA_HOME/bin/sh ...
- React 与 Redux 在生产环境中的实践总结
React 与 Redux 在生产环境中的实践总结 前段时间使用 React 与 Redux 重构了我们360netlab 的 开放数据平台.现将其中一些技术实践经验总结如下: Universal 渲 ...
随机推荐
- spoj - ACTIV - Activities
ACTIV - Activities Ana likes many activities. She likes acrobatics, alchemy, archery, art, Arabic da ...
- 1114. Boxes
1114. Boxes Time limit: 0.6 secondMemory limit: 64 MB N boxes are lined up in a sequence (1 ≤ N ≤ 20 ...
- python-mysql-replication原理分析
源码地址:https://github.com/noplay/python-mysql-replication 文件解析: ├── binlogstream.py ├── bitmap.py ├── ...
- Codeforces 855B:Marvolo Gaunt's Ring(枚举,前后缀)
B. Marvolo Gaunt's Ring Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaun ...
- ZOJ 3960:What Kind of Friends Are You?
What Kind of Friends Are You? Time Limit: 1 Second Memory Limit: 65536 KB Japari Park is a large zoo ...
- Java生成随机数的4种方式
Random Random 类诞生于 JDK 1.0,它产生的随机数是伪随机数,也就是有规则的随机数.Random 使用的随机算法为 linear congruential pseudorandom ...
- 【python】PyQt5 QAction 添加点击事件
def test(): #your function ui.yourQActionName.triggered.connect(lambda:test()) #添加lambda: 就不报错了
- Propensity Scores
目录 基本的概念 重要的结果 应用 Propensity Score Matching Stratification on the Propensity Score Inverse Probabili ...
- Chapter 22 Target Trial Emulation
目录 22.1 The target trial 22.2 Causal effects in randomized trails 22.3 Causal effects in observation ...
- python + pytest + allure生成测试报告
pytest结合allure生成测试报告 环境搭建 要安装java环境,版本要是jdk1.8的,配置好java环境变量,不然输入allure命令会报错,JAVA_HOME环境,自行配置 安装allur ...