【问题标题】:How to refactor helper methods with generics and foreign classes? (Java Android)如何用泛型和外部类重构辅助方法? (Java 安卓)
【发布时间】:2016-04-17 02:18:07
【问题描述】:

我正在构建一个 Android 应用程序并使用 ORMLite 进行 SQLite 操作,并希望创建帮助类来处理数据库。我遇到过大代码重复的问题,但不知道如何重构它们。
如果您对此有任何想法,请告知。
我觉得这个问题是因为缺乏一些基础知识,所以如果你能给我一个建议,关于我应该深入学习哪个主题,那就太棒了!

以下是重构中需要的代码块:

public static BigGoal createBigGoalRecord(String title,
                                          String description,
                                          Dao<BigGoal, Integer> dao) throws SQLException {
    BigGoal bigGoal = new BigGoal(title, description);
    dao.create(bigGoal);
    assignBigGoalEmptyCollection(bigGoal, dao);
    return bigGoal;
}

public static SubGoal createSubGoalRecord(String title, String description,
                                          ObjectiveType type,
                                          Dao<SubGoal, Integer> dao,
                                          BigGoal bigGoal) throws SQLException {
    SubGoal subGoal = bigGoal.createSubGoal(title, description, type);
    dao.create(subGoal);
    assignSubGoalEmptyCollection(subGoal, dao);
    bigGoal.getSubGoals().add(subGoal);
    return subGoal;
}

public static List<BigGoal> getBigGoalList (Dao<BigGoal, Integer> dao) throws SQLException {
    ArrayList<BigGoal> bigGoalList = new ArrayList<>();
    CloseableIterator<BigGoal> iterator = dao.closeableIterator();
    try {
        while (iterator.hasNext()){
            BigGoal goal = iterator.next();
            bigGoalList.add(goal);
        }
    } finally {
        iterator.close();
    }

    return bigGoalList;
}

public static List<SubGoal> getSubGoalList (Dao<SubGoal, Integer> dao) throws SQLException {
    ArrayList<SubGoal> subGoalList = new ArrayList<>();
    CloseableIterator<SubGoal> iterator = dao.closeableIterator();
    try {
        while (iterator.hasNext()){
            SubGoal goal = iterator.next();
            subGoalList.add(goal);
        }
    } finally {
        iterator.close();
    }

    return subGoalList;
}

【问题讨论】:

  • 看看这个example。它提供了一些基本的想法
  • 谢谢!这对我也有帮助。

标签: java android refactoring ormlite


【解决方案1】:

Oracle 网站在这里有一个关于 Java 泛型的完整部分:https://docs.oracle.com/javase/tutorial/java/generics/

例如,对于返回实体列表的方法(例如getBigGoalList()),您可以将它们全部替换为这个:

public static <T> List<T> getEntityList(Dao<T, Integer> dao) throws SQLException {
    ArrayList<T> list = new ArrayList<>();
    CloseableIterator<T> iterator = dao.closeableIterator();
    try {
        while (iterator.hasNext()){
            T item = iterator.next();
            list.add(item);
        }
    } finally {
        iterator.close();
    }

    return list;
}

我对 ORMLite 的了解还不够,无法告诉您相同类型的重构是否适用于在数据库中创建和保存实体的方法。您最好让这些方法将实体的实例作为参数,而不是采用用于构造实体的所有参数,即:

createBigGoalRecord(BigGoal item, Dao<BigGoal, Integer> dao)

而不是

createBigGoalRecord(String title, String description, Dao<BigGoal, Integer> dao)

否则我看不到将它们合并为一个方法的简单方法,因为它们似乎需要不同的参数。

【讨论】:

  • 这正是我要找的!非常感谢!
猜你喜欢
  • 2015-01-27
  • 1970-01-01
  • 2011-04-24
  • 2010-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-20
相关资源
最近更新 更多