title: Android - DAO Pattern
body: <p>I am implementing an Android application using DAO pattern. Althought from reading I have come to understand some principles (like the advantages regarding multiple data sources) I am not 100% sure that my implementation checks all the main goals of the pattern (ex: implementation hiding).</p>
  
  <p>If we need to completely hide the implementation details from the view, and since I am currently passing the application context to the constructor of TeamItemDataSource.java I think this should be reworked and I have two solutions in mind: one of the things I am currently thinking is extending SQLiteOpenHelper from the TeamIteamDataSource.java but I think I would get in trouble because I have already one class that does that work (and I would have to extend some more objects from SQLiteOpenHelper). On the other hand I could always pass the application context and in the factory method I simply would call the constructor of a potential future class that does not use application context. </p>
  
  <p>One last question regarding implementation hiding (and I assume I might be wrong about this concept - if so I would appreciate an explanation): 
  The way to hide implementation details inside the class that accesses the database (TeamItemDataSource.java) is by using private or protected strings containing the sql statements? Because if all the methods are public I am not hiding anything.</p>
  
  <p>For one object of my application (its about football (the non-USA one)) I have these classes:</p>
  
  <p><strong>TeamItemDAOFactory.java</strong></p>
  
  <pre><code>    public class TeamItemDAOFactory {
  
          private final static int SQLite = 1;
  
          public static TeamItemDAO getTeamItemDAO(Context ctx, int activeDB) {
              TeamItemDAO teamItemDAO;
              switch(activeDB) {
              case SQLite:
                  teamItemDAO = new TeamItemDataSource(ctx);
                  return teamItemDAO;
              default:
                  return null;
              }
  
          }
      }
  </code></pre>
  
  <p><strong>TeamItemDAO.java</strong></p>
  
  <pre><code>    public interface TeamItemDAO {
  
          public ArrayList&lt;TeamItem&gt; getAllTeamsByRanking();
  
          public void populateDBfromJSON(JSONArray jsonInput) throws SQLiteException;
  
          public void insertTeam(TeamItem teamItem) throws SQLiteException;
  
          public void updateTeam(TeamItem teamItem);
  
          public void deleteTeams();
      }
  </code></pre>
  
  <p><strong>TeamItemDataSource.java</strong></p>
  
  <pre><code>    public class TeamItemDataSource implements TeamItemDAO {
          private SQLiteDatabase database;
          private CIFDBHelper dbHelper;
          private String[] allColumns = { CIFDBHelper.COLUMN_EQUIPA,
                  CIFDBHelper.COLUMN_VITORIAS, CIFDBHelper.COLUMN_EMPATES,
                  CIFDBHelper.COLUMN_DERROTAS, CIFDBHelper.COLUMN_PONTOS,
                  CIFDBHelper.COLUMN_JOGOS, CIFDBHelper.COLUMN_LUGAR };
          private String TAG = "TeamItemDataSource";
  
          public TeamItemDataSource(Context context) {
              dbHelper = CIFDBHelper.getInstance(context);
          }
  
          private void open() throws SQLException {
              database = dbHelper.getWritableDatabase();
          }
  
          public void close() {
              dbHelper.close();
          }
  
          public ArrayList&lt;TeamItem&gt; getAllTeamsByRanking() {
              //
          }
  
          public void populateDBfromJSON(JSONArray jsonInput) throws SQLiteException {
              //
          }
  
          public void insertTeam(TeamItem teamItem) throws SQLiteException{
              //
          }
  
          public void updateTeam(TeamItem teamItem) {
              //
          }
  
          public void deleteTeams() {
              //
          }
  
          private TeamItem cursorToTeam(Cursor cursor) {
              //
          }
      }
  </code></pre>