如何用泛型和外来的 类 重构辅助方法? (Java Android)
How to refactor helper methods with generics and foreign classes? (Java Android)
我正在构建一个 Android 应用程序并使用 ORMLite 进行 SQLite 操作,并想创建帮助程序 class 来处理数据库。我遇到过大量重复代码的问题,但不知道如何重构它们。
有什么想法请指教
我觉得这个问题是因为缺乏一些基础知识,所以如果你能给我一个建议,我应该深入学习哪个主题,那就太棒了!
这里是重构需要的代码块:
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;
}
Oracle 网站在此处有一整节 Java 泛型:https://docs.oracle.com/javase/tutorial/java/generics/
例如,对于 return 实体列表(例如 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)
否则我看不到将它们合并为一个方法的简单方法,因为它们似乎需要不同的参数。
我正在构建一个 Android 应用程序并使用 ORMLite 进行 SQLite 操作,并想创建帮助程序 class 来处理数据库。我遇到过大量重复代码的问题,但不知道如何重构它们。
有什么想法请指教
我觉得这个问题是因为缺乏一些基础知识,所以如果你能给我一个建议,我应该深入学习哪个主题,那就太棒了!
这里是重构需要的代码块:
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;
}
Oracle 网站在此处有一整节 Java 泛型:https://docs.oracle.com/javase/tutorial/java/generics/
例如,对于 return 实体列表(例如 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)
否则我看不到将它们合并为一个方法的简单方法,因为它们似乎需要不同的参数。