私はAndroidアプリケーションを構築しており、SQLite操作にORMLiteを使用しており、データベース処理用のヘルパークラスを作成したいと考えています。私は大きなコードの重複の問題に直面しましたが、それらをリファクタリングする方法を理解することはできません。
ご意見がありましたらお知らせください。
基本的な知識が不足しているため、この問題があると感じています。アドバイスを送ることができれば、どのトピックをもっと深く学べばいいのでしょうか?ここで ジェネリックスや外国語のクラスでヘルパーメソッドをどのようにリファクタリングするのですか? (Java Android)
、のリストを返す、あなたのメソッドのために:
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;
}
この例をご覧ください(http://stackoverflow.com/questions/36506936/better-pattern-to-handle-dao-creation-for-pojo-using-sqlite/36511610#36511610)。それはいくつかの基本的なアイデアを提供します – jns
ありがとう!それが私にも役立ちます。 –