首先上实例代码
if(a){
//dosomething
}else if (b){
//doshomething
}else if(c){
//doshomething
} else{
////doshomething
}
这种大量的if else
嵌套,逻辑会比较混乱,并且很容易出错,比如这样的
if(SystemErrorCode.QUIT.getCode().equals(msg)){
System.out.println(SystemErrorCode.QUIT.getCode());
}else if(SystemErrorCode.ALL.getCode().equals(msg)){
System.out.println(SystemErrorCode.ALL.getCode());
}else if(SystemErrorCode.USER.getCode().equals(msg)){
System.out.println(SystemErrorCode.USER.getCode());
}else if(SystemErrorCode.ADMIN.getCode().equals(msg)){
System.out.println(SystemErrorCode.ADMIN.getCode());
}else if(SystemErrorCode.AI.getCode().equals(msg)){
System.out.println(SystemErrorCode.AI.getCode());
}else if(SystemErrorCode.QAI.getCode().equals(msg)){
System.out.println(SystemErrorCode.QAI.getCode());
}else if(SystemErrorCode.INFO.getCode().equals(msg)){
System.out.println(SystemErrorCode.INFO.getCode());
}else {
System.out.println("nothing");
}
刚开始条件比较少,现在功能多了,每次新增一个else条件都需要仔细的核对,防止对之前的逻辑有影响
这是修改过后的代码
InnerCommand instance = innerCommandContext.getInstance(msg);
instance.process(msg);
整体思路如下:
- 定义一个 InnerCommand 接口,其中有一个 process 函数交给具体的业务实现。
- 根据自己的业务,会有多个类实现 InnerCommand 接口;这些实现类都会注册到 SpringBean 容器中供之后使用。
- 通过客户端输入命令,从 SpringBean 容器中获取一个 InnerCommand 实例。
-
执行最终的 process 函数。
主要想实现的目的就是在有多个判断条件,只需要根据当前msg的状态动态的获取 InnerCommand 实例。
从源码上来看最主要的就是 InnerCommandContext 类,他会根据当前命令动态获取 InnerCommand 实例。
/**
* @ProjectName: aServerAdmin
* @Package: com.app
* @ClassName: InnerCommandContext
* @Description:求你了,写点注释吧
* @Author: Kevin
* @CreateDate: 18/2/15 下午3:32
* @UpdateUser:
* @UpdateDate: 18/2/15 下午3:32
* @UpdateRemark:
* @Version: 1.0
*/
public class InnerCommandContext {
@Autowired
ApplicationContext applicationContext;
public InnerCommand getInstance(String command) {
//getAllClazz
Map<String, String> allClazz = SystemErrorCode.getAllClazz();
String[] trim = command.trim().split(" ");
String clazz = allClazz.get(trim[0]);
InnerCommand innerCommand = null;
try {
if (StringUtils.isEmpty(clazz)) {
clazz = null;
}
innerCommand = (InnerCommand) applicationContext.getBean(Class.forName(clazz));
} catch (Exception e) {
e.printStackTrace();
}
return innerCommand;
}
}
- 第一步是获取所有的
InnerCommand
实例列表。 - 根据客户端输入的命令从第一步的实例列表中获取类类型。
- 根据类类型从
Spring
容器中获取具体实例对象。
/**
* @ClassName: SystemErrorCode
* @Description: 枚举
* @Author: Kevin
* @CreateDate: 18/2/15 下午5:31
* @UpdateUser:
* @UpdateDate: 18/2/15 下午5:31
* @UpdateRemark: 更新项目
* @Version: 1.0
*/
public enum SystemErrorCode {
QUIT(":quit", "", "PrintQuitCommand"),
ALL(":all", "", "PrintAllCommand"),
USER(":user", "", "PrintUserCommand"),
ADMIN(":admin", "", "PrintAdminCommand"),
AI(":ai", "", "PrintAiCommand"),
QAI(":qai", "", "PrintQaiCommand"),
INFO(":info", "", "PrintInfoCommand");
private String code;
private String desc;
private String clazz;
private static final Map<String, String> err_desc = new HashMap<String, String>();
static {
for (SystemErrorCode refer : SystemErrorCode.values()) {
err_desc.put(refer.getCode(), refer.getClazz());
}
}
private SystemErrorCode(String code, String desc, String clazz) {
this.code = code;
this.desc = desc;
this.clazz = clazz;
}
public static Map<String, String> getAllClazz() {
return err_desc;
}
public static String getDescByCode(int code) {
return err_desc.get(code);
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getClazz() {
return clazz;
}
public void setClazz(String clazz) {
this.clazz = clazz;
}
}
/**
* @ProjectName: aServerAdmin
* @Package: com.app.command
* @ClassName: InnerCommand
* @Description: 接口代码
* @Author: Kevin
* @CreateDate: 18/2/15 下午3:24
* @UpdateUser:
* @UpdateDate: 18/2/15 下午3:24
* @UpdateRemark:
* @Version: 1.0
*/
public interface InnerCommand {
void process(String msg);
}
/**
* @ProjectName: aServerAdmin
* @Package: com.app.service
* @ClassName: PrintAdminCommand
* @Description:示例
* @Author: Kevin
* @CreateDate: 18/2/15 下午5:06
* @UpdateUser:
* @UpdateDate: 18/2/15 下午5:06
* @UpdateRemark:
* @Version: 1.0
*/
@Service
public class PrintAdminCommand implements InnerCommand {
@Override
public void process(String msg) {
}
}