I am using an Android application using the DAO pattern. Although I understood some principles from reading (for example, the advantages with respect to several data sources), I am not 100% sure that my implementation checks all the main goals of the template (for example, hiding the implementation).
If you need to completely hide the implementation details from the point of view, and since then I am currently going through the application context to the TeamItemDataSource.java constructor I think this should be reworked and I have two solutions in the form: one of the things, I currently Thinking extends SQLiteOpenHelper from TeamIteamDataSource.java, but I think I would like to get in trouble because I already have one class that does the job (and I would extend a few more objects from SQLiteOpenHelper). On the other hand, I could always pass on the application context and in the factory method, I would just call the constructor of a potential future class that does not use the application context.
One final question about hiding the implementation (and I assume that I am mistaken in 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 to use private or protected strings containing sql statements? Because if all methods are open, I do not hide anything.
For one object of my application (its about football (not in the USA)) I have the following classes:
TeamItemDAOFactory.java
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;
}
}
}
TeamItemDAO.java
public interface TeamItemDAO {
public ArrayList<TeamItem> getAllTeamsByRanking();
public void populateDBfromJSON(JSONArray jsonInput) throws SQLiteException;
public void insertTeam(TeamItem teamItem) throws SQLiteException;
public void updateTeam(TeamItem teamItem);
public void deleteTeams();
}
TeamItemDataSource.java
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<TeamItem> 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) {
}
}