代码该如何重构设计一下,提高一下可拓展性

冰冰的冻结
  • 519
public class TyMbLabelManager {


    private static final String storageName = "TyMbStorage";
    public static final String studentLabelMb = "xsbqdm";
    public static final String teacherLabelMb = "jsbqdm";
    public static final String feedBackMb = "fklxdm";
    public static final String rechargeMethodMb = "czfsdm";

    private Map<String, String> studentLabelMap = new HashMap<>();
    private Map<String, String> teacherLabelMap = new HashMap<>();
    private Map<String, String> feedBackMap = new HashMap<>();
    private Map<String, String> rechargeMethodMap = new HashMap<>();

    private static Map<String, Map<String, String>> mbMap = new HashMap<>();

    private static TyMbLabelManager instance = null;

    private TyMbLabelManager() {
        mbMap.put(studentLabelMb, studentLabelMap);
        mbMap.put(teacherLabelMb, teacherLabelMap);
        mbMap.put(feedBackMb, feedBackMap);
        mbMap.put(rechargeMethodMb, rechargeMethodMap);
    }

    public static TyMbLabelManager getInstance() {
        if (instance == null) {
            synchronized (TyMbLabelManager.class) {
                instance = new TyMbLabelManager();
            }
        }
        return instance;
    }



    public void getMbLabel(final String code) {
        GetLabelMB.InParam inParam = new GetLabelMB.InParam();
        inParam.set_codeclass(code);
        GetLabelMB.execute(inParam, new GetLabelMB.ResultListener() {
            @Override
            public void onError(String s) {
                Logger.getLogger().e("获取码表标签失败:" + s);
            }

            @Override
            public void onSuccess(GetLabelMB.OutParam outParam) {
                if (outParam != null && TyMathConstant.SUCCESSFUL.equals(outParam.get_isSuccess())) {
                    saveMb(outParam.get_data(), code);
                    getMbInfo();
                }
            }
        });
    }

    private void saveMb(ArrayList<GetLabelMB.Data_sub> data, String codeMb) {
        Gson gson = GsonUtil.getInstance();
        ArrayList<GetLabelMB.Data_sub> codeList = new ArrayList<>();
        String[] codeArray = codeMb.split(",");
        for (int i = 0; i < codeArray.length; i++) {
            String code = codeArray[i];
            codeList.clear();
            for (int j = 0; j < data.size(); j++) {
                GetLabelMB.Data_sub mb = data.get(j);
                if (code.equalsIgnoreCase(mb.get_codeclass())) {
                    codeList.add(mb);
                }
            }
            SPStoreUtil.putString(storageName, code, gson.toJson(codeList));
        }
    }


    public void getMbInfo() {
        Gson gson = GsonUtil.getInstance();
        for (Map.Entry<String, Map<String, String>> entry : mbMap.entrySet()) {

            String json = SPStoreUtil.getString(storageName, entry.getKey());

            if (TextUtils.isEmpty(json)) {
                continue;
            }

            ArrayList<GetLabelMB.Data_sub> mbList = gson.fromJson(json,
                    new TypeToken<List<GetLabelMB.Data_sub>>(){}.getType());
            if (mbList == null || mbList.isEmpty()) {
                continue;
            }
            putMb(mbList, entry.getValue());
        }
    }

    private void putMb(ArrayList<GetLabelMB.Data_sub> studentLabelList, Map<String, String> mbMap) {
        mbMap.clear();
        if (studentLabelList != null) {
            for (int i = 0; i < studentLabelList.size(); i++) {
                GetLabelMB.Data_sub mb = studentLabelList.get(i);
                mbMap.put(mb.get_key(), mb.get_value());
            }
        }
    }

    public String getStudentMbLabel(String key) {
        String value = "";
        if (TextUtils.isEmpty(key)) {
            return value;
        }
        value = studentLabelMap.get(key);
        return value;
    }

    public String getTeacherMbLabel(String key) {
        String value = "";
        if (TextUtils.isEmpty(key)) {
            return value;
        }
        value = teacherLabelMap.get(key);
        return value;
    }

    public String getFeedBackMb(String key) {
        String value = "";
        if (TextUtils.isEmpty(key)) {
            return value;
        }
        value = feedBackMap.get(key);
        return value;
    }

    public String getRechargeMethodMb(String key) {
        String value = "";
        if (TextUtils.isEmpty(key)) {
            return value;
        }
        value = rechargeMethodMap.get(key);
        return value;
    }

    public Map<String, String> getStudentLabelMap(){
        return studentLabelMap;
    }

    public Map<String, String> getTeacherLabelMap(){
        return teacherLabelMap;
    }

    public Map<String, String> getFeedBackMap() {
        return feedBackMap;
    }

    public Map<String, String> getRechargeMethodMap() {
        return rechargeMethodMap;
    }
}
回复
阅读 1.1k
1 个回答
炒蛋
  • 51

代码能合并的合并,方法可以复用的 复用,封装的封装

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏