2016-04-16 6 views
1

私はAndroidアプリケーションを構築しており、SQLite操作にORMLiteを使用しており、データベース処理用のヘルパークラスを作成したいと考えています。私は大きなコードの重複の問題に直面しましたが、それらをリファクタリングする方法を理解することはできません。
ご意見がありましたらお知らせください。
基本的な知識が不足しているため、この問題があると感じています。アドバイスを送ることができれば、どのトピックをもっと深く学べばいいのでしょうか?ここで ジェネリックスや外国語のクラスでヘルパーメソッドをどのようにリファクタリングするのですか? (Java Android)

は、コードブロックでは、リファクタリングに必要があります。例として https://docs.oracle.com/javase/tutorial/java/generics/

、のリストを返す、あなたのメソッドのために:

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; 
} 
+0

この例をご覧ください(http://stackoverflow.com/questions/36506936/better-pattern-to-handle-dao-creation-for-pojo-using-sqlite/36511610#36511610)。それはいくつかの基本的なアイデアを提供します – jns

+0

ありがとう!それが私にも役立ちます。 –

答えて

0

OracleのWebサイトは、ここでのJavaのジェネリックのセクション全体を持っていますエンティティ(例えばgetBigGoalList())、あなたはこの1でそれらのすべてを置き換えることができます:

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 yを伝えることについて十分に知りません同じ種類のリファクタリングが、データベース内のエンティティを作成して保存するメソッドに対して機能するかどうか。それ以外の場合は

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

代わり

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

の:あなたは、これらの方法ではなく、私、電子エンティティを構築するために使用されるすべての引数を取るのパラメータとしてエンティティのインスタンスを取るた方が良いかもしれません彼らは異なる議論を必要とするように見えるので、私はそれらを一つの方法に統合する簡単な方法は見当たりません。

+0

これはまさに、私が探しているものです!どうもありがとうございました! –

関連する問題