通用技术 [java] 小学生代码优化之枚举类型使用

bauul · 2017年06月19日 · 746 次阅读

被 BS 的代码

最近做一个 android 和 ios 关键字切换的代码,最初写了一个 switch 切换的代码,然后有二三十个关键字要做切换,结果代码就超长

switch (id) {
case "appiumStart":
        param.put("id", "iosAppiumStart");
        param.put("methodName", "cn.carl.test:iosAppiumStart");
        break;
case "appiumStop":
        param.put("id", "iosAppiumStop");
        param.put("methodName", "cn.carl.test:iosAppiumStop");
        break;
......

修改思路

修改代码的灵感来源于一个前同事写的枚举类型,代码如下,

public enum SwipeDirection {
    up("上"),down("下"),left("左"),right("右");
    private String mDisplayName;
    private static final Map<String, SwipeDirection> nameToEnum;

    private SwipeDirection(String psName)
    {
        mDisplayName=psName;
    }
    public String getDisplayName()
    {
        return this.mDisplayName;
    }
    public static String getOrdinalValue(String name) {
        SwipeDirection type = SwipeDirection.valueOf(name);
        String result = "";
        switch (type.ordinal()) {
        case 0:
            result = "上";
            break;
        case 1:
            result = "下";
            break;
        case 2:
            result = "左";
            break;
        case 3:
            result = "右";
            break;
        }
        return result;
    }
  public static SwipeDirection fromName(String name) {
      return (SwipeDirection)nameToEnum.get(name);
  }

  static
  {
      nameToEnum = new HashMap<String, SwipeDirection>();

   for (SwipeDirection type : values())
       nameToEnum.put(type.mDisplayName, type);
  }
}

定义枚举类型,并将值放到 map 对象中,然后调用,不过这个代码并不好,主要 map 的 value 用的 SwipeDirection,且仍然有用 switch,所以我自己又修改了一下

我的修改后

代码比较简单,也没啥好详细解释的了,就不细说了吧

public enum SwitchAndroidKeywordToIos {
    appiumStart("iosAppiumStart", "cn.carl.test:iosAppiumStart"),
    appiumStop("iosAppiumStop", "cn.carl.test:iosAppiumStop");

    private static Map<String, String> keywords = new HashMap<String, String>();
    private String id;
    private String methodName;

    private SwitchAndroidKeywordToIos(String id, String methodName) {
        this.id = id;
        this.methodName = methodName;
    }

    public String getId() {
        return id;
    }

    public String getMethodName() {
        return methodName;
    }

    static {
        for (SwitchAndroidKeywordToIos keyword : values()) {
           keywords.put(keyword.id, keyword.methodName);
        }
    }

    public static boolean containsKeywordId(String keywordId) {
        for (SwitchAndroidKeywordToIos s: values()) {
            if (s.name().equals(keywordId)) {
                return true;
            }
        }
        return false;
    }

    public static String getSwitchedKeywordId(String keywordId) {
        for (SwitchAndroidKeywordToIos s: values()) {
            if (s.name().equals(keywordId)) {
                return s.id;
            }
        }
        return null;
    }

    public static String getSwitchedKeywordMethodName(String keywordId) {
        for (SwitchAndroidKeywordToIos s: values()) {
            if (s.name().equals(keywordId)) {
                return s.methodName;
            }
        }
        return null;
    }
}

后感

星期六日改了好几遍,总算让自己比较满意了

参考书籍

Effective Java 中文版 第 2 版(第六章)

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册