前言
大家在Coding的时候,经常会遇到这样一个情况,根据不同的条件去执行对应的代码。我们通常的处理方式是利用if-else判断,或者直接switch-case,特别是jdk1.6之后,swith开始支持判断String类型了。
但是如果有几十种情况上百种情况,用if-else来处理这段逻辑,代码看着特别不舒服。
解决这个麻烦有好几种办法,比如自定义注解、Spring结合策略模式、反射或者枚举。
下面分享一下使用Guava工具类通过反射动态调用方法。
业务描述
插座统计:数据库每添加一个插座实例,都统计一下这个插座属于中国哪个省份(细节不重要)
pom.xml
添加工具依赖
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
ProvinceEnum.java
前端传过来的是中文,需要获取对应的英文名,然后通过拼接的方式生成方法名。
public enum ProvinceEnum { SHANDONG("山东", "Shandong"), SHANGHAI("上海", "Shanghai"), ZHEJIANG("浙江", "Zhejiang"), ANHUI("安徽", "Anhui"), FUJIAN("福建", "Fujian"), JIANGXI("江西", "Jiangxi"), GUANGDONG("广东", "Guangdong"), GUANGXI("广西", "Guangxi"), HAINAN("海南", "Hainan"), HENAN("河南", "Henan"), HUNAN("湖南", "Hunan"), HUBEI("湖北", "Hubei"), BEIJING("北京", "Beijing"), TIANJIN("天津", "Tianjin"), HEBEI("河北", "Hebei"), SHANXI("山西", "Shanxi"), INNER*("内蒙古", "Inner*"), NINGXIA("宁夏", "Ningxia"), QINGHAI("青海", "Qinghai"), SHAANXI("陕西", "Shaanxi"), GANSU("甘肃", "Gansu"), SINKIANG("*", "Sinkiang"), SICHUAN("四川", "Sichuan"), GUIZHOU("贵州", "Guizhou"), YUNNAN("云南", "Yunnan"), CHONGQING("重庆", "Chongqing"), TIBET("*", "Tibet"), LIAONING("辽宁", "Liaoning"), JILIN("吉林", "Jilin"), HEILONGJIANG("黑龙江", "Heilongjiang"), HONGKONG("香港", "Hongkong"), MACAO("澳门", "Macao"), *("*", "*"), JIANGSU("江苏", "Jiangsu"); private String code; private String msg; private ProvinceEnum(String code, String msg) {
this.code = code;
this.msg = msg;
} public String getCode() {
return code;
} public String getMsg() {
return msg;
} public static String getMsgByCode(String code) {
String msg = null;
for (ProvinceEnum e : ProvinceEnum.values()) {
if (e.getCode().equals(code)) {
msg = e.getMsg();
break;
}
}
return msg;
} }
点击展开
SocketStatistics.java
实体类,统计的对象
public class SocketStatistics {
/**
* 主键自增长
*/
private Long pid; private Integer shandong; private Integer jiangsu; private Integer shanghai; private Integer zhejiang; private Integer anhui; private Integer fujian; private Integer jiangxi; private Integer guangdong; private Integer guangxi; private Integer hainan; private Integer henan; private Integer hunan; private Integer hubei; private Integer beijing; private Integer tianjin; private Integer hebei; private Integer shanxi; /**
* 内蒙古
*/
private Integer inner*; private Integer ningxia; private Integer qinghai; /**
* 陕西
*/
private Integer shaanxi; private Integer gansu; private Integer sinkiang; private Integer sichuan; private Integer guizhou; private Integer yunnan; private Integer chongqing; /**
* *
*/
private Integer tibet; private Integer liaoning; private Integer jilin; private Integer heilongjiang; private Integer hongkong; /**
* 澳门
*/
private Integer macao; private Integer *; /**
* 在线数量
*/
private Integer online; /**
* 离线数量
*/
private Integer outline; public Long getPid() {
return pid;
} public void setPid(Long pid) {
this.pid = pid;
} public Integer getShandong() {
return shandong;
} public void setShandong(Integer shandong) {
this.shandong = shandong;
} public Integer getJiangsu() {
return jiangsu;
} public void setJiangsu(Integer jiangsu) {
this.jiangsu = jiangsu;
} public Integer getShanghai() {
return shanghai;
} public void setShanghai(Integer shanghai) {
this.shanghai = shanghai;
} public Integer getZhejiang() {
return zhejiang;
} public void setZhejiang(Integer zhejiang) {
this.zhejiang = zhejiang;
} public Integer getAnhui() {
return anhui;
} public void setAnhui(Integer anhui) {
this.anhui = anhui;
} public Integer getFujian() {
return fujian;
} public void setFujian(Integer fujian) {
this.fujian = fujian;
} public Integer getJiangxi() {
return jiangxi;
} public void setJiangxi(Integer jiangxi) {
this.jiangxi = jiangxi;
} public Integer getGuangdong() {
return guangdong;
} public void setGuangdong(Integer guangdong) {
this.guangdong = guangdong;
} public Integer getGuangxi() {
return guangxi;
} public void setGuangxi(Integer guangxi) {
this.guangxi = guangxi;
} public Integer getHainan() {
return hainan;
} public void setHainan(Integer hainan) {
this.hainan = hainan;
} public Integer getHenan() {
return henan;
} public void setHenan(Integer henan) {
this.henan = henan;
} public Integer getHunan() {
return hunan;
} public void setHunan(Integer hunan) {
this.hunan = hunan;
} public Integer getHubei() {
return hubei;
} public void setHubei(Integer hubei) {
this.hubei = hubei;
} public Integer getBeijing() {
return beijing;
} public void setBeijing(Integer beijing) {
this.beijing = beijing;
} public Integer getTianjin() {
return tianjin;
} public void setTianjin(Integer tianjin) {
this.tianjin = tianjin;
} public Integer getHebei() {
return hebei;
} public void setHebei(Integer hebei) {
this.hebei = hebei;
} public Integer getShanxi() {
return shanxi;
} public void setShanxi(Integer shanxi) {
this.shanxi = shanxi;
} public Integer getInner*() {
return inner*;
} public void setInner*(Integer inner*) {
this.inner* = inner*;
} public Integer getNingxia() {
return ningxia;
} public void setNingxia(Integer ningxia) {
this.ningxia = ningxia;
} public Integer getQinghai() {
return qinghai;
} public void setQinghai(Integer qinghai) {
this.qinghai = qinghai;
} public Integer getShaanxi() {
return shaanxi;
} public void setShaanxi(Integer shaanxi) {
this.shaanxi = shaanxi;
} public Integer getGansu() {
return gansu;
} public void setGansu(Integer gansu) {
this.gansu = gansu;
} public Integer getSinkiang() {
return sinkiang;
} public void setSinkiang(Integer sinkiang) {
this.sinkiang = sinkiang;
} public Integer getSichuan() {
return sichuan;
} public void setSichuan(Integer sichuan) {
this.sichuan = sichuan;
} public Integer getGuizhou() {
return guizhou;
} public void setGuizhou(Integer guizhou) {
this.guizhou = guizhou;
} public Integer getYunnan() {
return yunnan;
} public void setYunnan(Integer yunnan) {
this.yunnan = yunnan;
} public Integer getChongqing() {
return chongqing;
} public void setChongqing(Integer chongqing) {
this.chongqing = chongqing;
} public Integer getTibet() {
return tibet;
} public void setTibet(Integer tibet) {
this.tibet = tibet;
} public Integer getLiaoning() {
return liaoning;
} public void setLiaoning(Integer liaoning) {
this.liaoning = liaoning;
} public Integer getJilin() {
return jilin;
} public void setJilin(Integer jilin) {
this.jilin = jilin;
} public Integer getHeilongjiang() {
return heilongjiang;
} public void setHeilongjiang(Integer heilongjiang) {
this.heilongjiang = heilongjiang;
} public Integer getHongkong() {
return hongkong;
} public void setHongkong(Integer hongkong) {
this.hongkong = hongkong;
} public Integer getMacao() {
return macao;
} public void setMacao(Integer macao) {
this.macao = macao;
} public Integer get*() {
return *;
} public void set*(Integer *) {
this.* = *;
} public Integer getOnline() {
return online;
} public void setOnline(Integer online) {
this.online = online;
} public Integer getOutline() {
return outline;
} public void setOutline(Integer outline) {
this.outline = outline;
} public void addTibet() {
this.tibet = this.tibet + 1;
} /**
* 山东+1
*
*/
public void addShandong() {
this.shandong = this.shandong + 1;
} /**
* 江苏+1
*
*/
public void addJiangsu() {
this.jiangsu = this.jiangsu + 1;
} /**
* 上海+1
*
*/
public void addShanghai() {
this.shanghai = this.shanghai + 1;
} /**
* 浙江+1
*
*/
public void addZhejiang() {
this.zhejiang = this.zhejiang + 1;
} /**
* 安徽+1
*
*/
public void addAnhui() {
this.anhui = this.anhui + 1;
} /**
* 福建+1
*
*/
public void addFujian() {
this.fujian = this.fujian + 1;
} /**
* 江西+1
*
*/
public void addJiangxi() {
this.jiangxi = this.jiangxi + 1;
} /**
* 广东+1
*
*/
public void addGuangdong() {
this.guangdong = this.guangdong + 1;
} /**
* 广西+1
*
*/
public void addGuangxi() {
this.guangxi = this.guangxi + 1;
} /**
* 海南+1
*
*/
public void addHainan() {
this.hainan = this.hainan + 1;
} /**
* 河南+1
*
*/
public void addHenan() {
this.henan = this.henan + 1;
} /**
* 湖南+1
*
*/
public void addHunan() {
this.hunan = this.hunan + 1;
} /**
* 湖北+1
*
*/
public void addHubei() {
this.hubei = this.hubei + 1;
} /**
* 北京+1
*
*/
public void addBeijing() {
this.beijing = this.beijing + 1;
} /**
* 天津+1
*
*/
public void addTianjin() {
this.tianjin = this.tianjin + 1;
} /**
* 河北+1
*
*/
public void addHebei() {
this.hebei = this.hebei + 1;
} /**
* 山西+1
*
*/
public void addShanxi() {
this.shanxi = this.shanxi + 1;
} /**
* 陕西+1
*
*/
public void addShaanxi() {
this.shaanxi = this.shaanxi + 1;
} /**
* 内蒙古+1
*
*/
public void addInner*() {
this.inner* = this.inner* + 1;
} /**
* 宁夏+1
*
*/
public void addNingxia() {
this.ningxia = this.ningxia + 1;
} /**
* 青海+1
*
*/
public void addQinghai() {
this.qinghai = this.qinghai + 1;
} /**
* 甘肃+1
*
*/
public void addGansu() {
this.gansu = this.gansu + 1;
} /**
* *+1
*
*/
public void addSinkiang() {
this.sinkiang = this.sinkiang + 1;
} /**
* 四川+1
*
*/
public void addSichuan() {
this.sichuan = this.sichuan + 1;
} /**
* 贵州+1
*
*/
public void addGuizhou() {
this.guizhou = this.guizhou + 1;
} /**
* 云南+1
*
*/
public void addYunnan() {
this.yunnan = this.yunnan + 1;
} /**
* 重庆+1
*
*/
public void addChongqing() {
this.chongqing = this.chongqing + 1;
} /**
* 辽宁+1
*
*/
public void addLiaoning() {
this.liaoning = this.liaoning + 1;
} /**
* 吉林+1
*
*/
public void addJilin() {
this.jilin = this.jilin + 1;
} /**
* 黑龙江+1
*
*/
public void addHeilongjiang() {
this.heilongjiang = this.heilongjiang + 1;
} /**
* 香港+1
*
*/
public void addHongkong() {
this.hongkong = this.hongkong + 1;
} /**
* 澳门+1
*
*/
public void addMacao() {
this.macao = this.macao + 1;
} /**
* *+1
*
*/
public void add*() {
this.* = this.* + 1;
}
}
点击展开
service层逻辑
// 从数据库中取出插座统计记录,该表中只有一条记录,如有变化直接更新就是
SocketStatistics socketStatistics = socketStatisticsMapper.selectOne(); // 根据前端传过来的插座所属省份中文名,到省份枚举类中得到对应的英文名
// 再通过拼接的方式得到对应的方法名
String methodName = "add"
+ ProvinceEnum.getMsgByCode(socket.getProvince()); // 到SocketStatistics类中找到这个方法
Method method = SocketStatistics.class.getDeclaredMethod(methodName); // Guava的工具类通过反射,动态调用该方法
Invokable<SocketStatistics, Object> invokable = (Invokable<SocketStatistics, Object>) Invokable
.from(method);
invokable.invoke(socketStatistics); // 数据处理完后,更新数据库
socketStatisticsMapper.updateByPrimaryKeySelective(socketStatistics);