java--⼤量switch-case代码加⼊枚举类型进⾏优化
当项⽬存在⼤量的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;java switch case string
private String trsDDzName;
private String wkeCodName;
FileName(String jrnDDZName, String trsDDzName, String wkeCodName) {  this.jrnDDZName = jrnDDZName;
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 = sDDzName();
wkeCodName = valueOf.wkeCodName();
⼤⼤简化了代码量,也看起来更加简洁,更容易维护。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。