最近在重构同事写的代码,代码设计的非常好,由于涉及到的类比较多,我这里就以一个枚举类来记录关于枚举和字符串的优化(不涉及到业务部分)
代码如下:
package com.vcg.community.data.constant;
public enum InvokerTypeEnum {
//读操作
ReadEntityById {
public String cmdClassName() {return "com.vcg.community.data.command.read.EntityByIdCmd";}
},
ReadEntityByIndex {
public String cmdClassName() {return "com.vcg.community.data.command.read.EntityByIndexCmd";}
},
ReadEntityByLink{
public String cmdClassName() {return "com.vcg.community.data.command.read.EntityByLinkCmd";}
},
ReadEntityByColumn{
public String cmdClassName() {return "com.vcg.community.data.command.read.EntityByColumnCmd";}
},
ReadCount {
public String cmdClassName() {return "com.vcg.community.data.command.read.CountCmd";}
},
ReadCountByLink {
public String cmdClassName() {return "com.vcg.community.data.command.read.CntByLinkCmd";}
},
ReadStateByLink {
public String cmdClassName() {return "com.vcg.community.data.command.read.StateByLinkCmd";}
},
Search {
public String cmdClassName() {return "com.vcg.community.data.command.read.SearchCmd";}
},
Msg {
public String cmdClassName() {return "com.vcg.community.data.command.read.MsgCmd";}
},
ReadEntityBySql {
public String cmdClassName() {return "com.vcg.community.data.command.read.EntityBySqlCmd";}
},
SearchBySql {
public String cmdClassName() {return "com.vcg.community.data.command.read.SearchBySqlCmd";}
},
QueryCountBySql {
public String cmdClassName() {return "com.vcg.community.data.command.read.QueryCountBySqlCmd";}
},
QueryIdsBySql {
public String cmdClassName() {return "com.vcg.community.data.command.read.QueryIdsBySqlCmd";}
},
//写操作
InsertEntitys{
public String cmdClassName() {return "com.vcg.community.data.command.write.InsertEntitysCmd";}
},
DeleteEntitys{
public String cmdClassName() {return "com.vcg.community.data.command.write.DeleteEntitysCmd";}
},
DoLinks{
public String cmdClassName() {return "com.vcg.community.data.command.write.DoLinksCmd";}
},
UndoLinks{
public String cmdClassName() {return "com.vcg.community.data.command.write.UndoLinksCmd";}
},
WriteOther{
public String cmdClassName() {return "com.vcg.community.data.command.write.WriteOtherCmd";}
},
ChangeCount{
public String cmdClassName() {return "com.vcg.community.data.command.write.ChangeCountCmd";}
};
public abstract String cmdClassName() ;
}
上下文代码
InvokerTypeEnum invokeType = inputObject.getInvokeType();
if (invokeType == null) {
return new OutputObject(OutputObject.CODE_ERROR, "the attr invoke_type can not be null");
}
String className = invokeType.cmdClassName();
AbstractCommand dp = (AbstractCommand) SpringUtils.getBeanByClassName(className);
outputObject = dp.checkInputAttr(inputObject);
if (outputObject.getCode() == OutputObject.CODE_ERROR) {
return outputObject;
}
outputObject = dp.execute(inputObject);
return outputObject;
}
public abstract class AbstractCommand {
@Autowired
protected RedisStrategy redisStrategy;
@Autowired
protected MysqlStrategy mysqlStrategy;
@Autowired
protected SearchStrategy searchStrategy;
public abstract OutputObject execute(InputObject inputObject);
public abstract OutputObject checkInputAttr(InputObject inputObject);
}
优化后的代码:
public enum InvokerType {
//读操作
ReadEntityById,
ReadEntityByIndex,
ReadEntityByLink,
ReadEntityByColumn,
ReadCount,
ReadCountByLink,
ReadStateByLink,
Search,
Msg,
ReadEntityBySql,
SearchBySql,
QueryCountBySql,
QueryIdsBySql,
//写操作
InsertEntitys,
DeleteEntity,
DoLinks,
UndoLinks,
WriteOther,
ChangeCount;
String cmdClassName(){return this.getClass().getPackage().getName()+".command."+this.name()+"Cmd";}
}
这样一来,是不是更合理了呢?我们来分析一下
1.首先从代码的数量来说,是不是更精简了一些。
2.形式来说,避开了抽象枚举,是不是更接近普通的枚举设计。
3.性能来说,同事的方式会让JVM产生更多的类似“com.vcg.community.data.command.read/write.xxxxx”的字符串常量(不是new出来的字符串都会在编译期间存入常量池中)
4.系统扩展性,当系统可能会新增该枚举类型时,优化后的方式更利于系统扩展,仅需新增一个枚举值即可。
5.系统稳定性,减少可能由于字符输入(大小写,非复制手敲等原因)错误而造成spring无法加载模板实例的可能性
最终,同事在我的说服下同意了我的修改。
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于