首先上實(shí)例代碼
if(a){
//dosomething
}else if (b){
//doshomething
}else if(c){
//doshomething
} else{
////doshomething
}
這種大量的if else
嵌套,邏輯會(huì)比較混亂帅刀,并且很容易出錯(cuò)让腹,比如這樣的
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");
}
剛開始條件比較少远剩,現(xiàn)在功能多了,每次新增一個(gè)else條件都需要仔細(xì)的核對骇窍,防止對之前的邏輯有影響
這是修改過后的代碼
InnerCommand instance = innerCommandContext.getInstance(msg);
instance.process(msg);
整體思路如下:
- 定義一個(gè) InnerCommand 接口瓜晤,其中有一個(gè) process 函數(shù)交給具體的業(yè)務(wù)實(shí)現(xiàn)。
- 根據(jù)自己的業(yè)務(wù)腹纳,會(huì)有多個(gè)類實(shí)現(xiàn) InnerCommand 接口痢掠;這些實(shí)現(xiàn)類都會(huì)注冊到 SpringBean 容器中供之后使用。
- 通過客戶端輸入命令嘲恍,從 SpringBean 容器中獲取一個(gè) InnerCommand 實(shí)例足画。
-
執(zhí)行最終的 process 函數(shù)。
主要想實(shí)現(xiàn)的目的就是在有多個(gè)判斷條件佃牛,只需要根據(jù)當(dāng)前msg的狀態(tài)動(dòng)態(tài)的獲取 InnerCommand 實(shí)例淹辞。
從源碼上來看最主要的就是 InnerCommandContext 類,他會(huì)根據(jù)當(dāng)前命令動(dòng)態(tài)獲取 InnerCommand 實(shí)例俘侠。
/**
* @ProjectName: aServerAdmin
* @Package: com.app
* @ClassName: InnerCommandContext
* @Description:求你了象缀,寫點(diǎn)注釋吧
* @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
實(shí)例列表。 - 根據(jù)客戶端輸入的命令從第一步的實(shí)例列表中獲取類類型兼贡。
- 根據(jù)類類型從
Spring
容器中獲取具體實(shí)例對象攻冷。
/**
* @ClassName: SystemErrorCode
* @Description: 枚舉
* @Author: Kevin
* @CreateDate: 18/2/15 下午5:31
* @UpdateUser:
* @UpdateDate: 18/2/15 下午5:31
* @UpdateRemark: 更新項(xiàng)目
* @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) {
}
}