当项目存在大量的switch-case条件判断时,会把让整个项目看起来非常low,以下tich提出一种优化方法,可提高代码的维护性,让代码变得更加简洁,易维护。 先看原代码: String jrnDDZName = null; String ...
当项目存在大量的switch-case条件判断时,会把让整个项目看起来非常low,以下tich提出一种优化方法,可提高代码的维护性,让代码变得更加简洁,易维护。
先看原代码:
String jrnDDZName = null;
String trsDDzName = null;
String wkeCodName = null;
switch (fileName)
{
case "FMCHKRULP":
jrnDDZName = "BFMSYNRUX1";
trsDDzName = "BFMSYNRUX2";
wkeCodName = "BFMSYNRU";
break;
case "FMCHKRALP":
jrnDDZName = "BFMSYNRAX1";
trsDDzName = "BFMSYNRAX2";
wkeCodName = "BFMSYNRA";
break;
case "FMBACDTAP":
jrnDDZName = "BFMSYNBCX1";
trsDDzName = "BFMSYNBCX2";
wkeCodName = "BFMSYNBC";
break;
case "FMLECDTAP":
jrnDDZName = "BFMSYNLCX1";
trsDDzName = "BFMSYNLCX2";
wkeCodName = "BFMSYNLC";
break;
case "FMLERDEFP":
jrnDDZName = "BFMSYNLRX1";
trsDDzName = "BFMSYNLRX2";
wkeCodName = "BFMSYNLR";
break;
//pgwyx 20180419
case "FMNTFDTAP":
jrnDDZName = "BFMSYNNFX1";
trsDDzName = "BFMSYNNFX2";
wkeCodName = "BFMSYNNF";
break;
case "FMCHKTMLP":
jrnDDZName = "BFMSYNTMX1";
trsDDzName = "BFMSYNTMX2";
wkeCodName = "BFMSYNTM";
break;
default:
break;
}
使用枚举:
public enum FileName {
FMCHKRULP("BFMSYNRUX1","BFMSYNRUX2","BFMSYNRU"),
FMCHKRALP("BFMSYNRUX2","BFMSYNRAX2","BFMSYNRA"),
FMBACDTAP("BFMSYNBCX1","BFMSYNBCX2","BFMSYNBC"),
FMLECDTAP("BFMSYNLCX1","BFMSYNLCX2","BFMSYNLC"),
FMLERDEFP("BFMSYNLRX1","BFMSYNLRX2","BFMSYNLR"),
FMNTFDTAP("BFMSYNNFX1","BFMSYNNFX2","BFMSYNNF"),
FMCHKTMLP("BFMSYNTMX1","BFMSYNTMX2","BFMSYNTM");
private String jrnDDZName;
private String trsDDzName;
private String wkeCodName;
FileName(String jrnDDZName, String trsDDzName, String wkeCodName) {
this.jrnDDZName = jrnDDZName;
this.trsDDzName = trsDDzName;
this.wkeCodName = wkeCodName;
}
public String jrnDDZName() {
return jrnDDZName;
}
public String trsDDzName() {
return trsDDzName;
}
public String wkeCodName() {
return wkeCodName;
}
}
简化后代码:
FileName valueOf = Enum.valueOf(FileName.class, fileName);
jrnDDZName = valueOf.jrnDDZName();
trsDDzName = valueOf.trsDDzName();
wkeCodName = valueOf.wkeCodName();
大大简化了代码量,也看起来更加简洁,更容易维护。
本站为非盈利网站,如果您喜欢这篇文章,欢迎支持我们继续运营!
本站主要用于日常笔记的记录和生活日志。本站不保证所有内容信息可靠!(大多数文章属于搬运!)如有版权问题,请联系我立即删除:“abcdsjx@126.com”。
QQ: 1164453243
邮箱: abcdsjx@126.com