【发布时间】: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