设计模式专家Android架构java 设计

代码调优——利用策略模式优化过多 if else 代码

2019-02-15  本文已影响85人  kevin0016

首先上实例代码

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);

整体思路如下:

/**
 * @ProjectName: aServerAdmin
 * @Package: com.app
 * @ClassName: InnerCommandContext
 * @Description:
 * @Author: Kevin
 * @CreateDate: 19/2/15 下午3:32
 * @UpdateUser:
 * @UpdateDate: 19/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;
    }
}
/**
 * @ClassName: SystemErrorCode
 * @Description: 枚举
 * @Author: Kevin
 * @CreateDate: 19/2/15 下午5:31
 * @UpdateUser:
 * @UpdateDate: 19/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: 19/2/15 下午3:24
 * @UpdateUser:
 * @UpdateDate: 19/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: 19/2/15 下午5:06
 * @UpdateUser:
 * @UpdateDate: 19/2/15 下午5:06
 * @UpdateRemark:
 * @Version: 1.0
 */
@Service
public class PrintAdminCommand implements InnerCommand {
    @Override
    public void process(String msg) {

    }
}

上一篇下一篇

猜你喜欢

热点阅读