DBAdapter design question
I'm developin开发者_JAVA技巧g an Android application with a database. That database will have more than three tables.
Now I'm working on a class called DBAdapter
to access the SQLite Database.
DBAdpater will have five methods for every table on database (insertEntry, removeEntry, getAllEntries, getEntry
and updateEntry
).
So, if I have five table, DBApadter will have more than twenty-five methods. I think is so huge.
How may DBAdapter classes should be?
Thanks.
You should be able to keep it to 5 public methods and a single DBAdapter class.
Create an ENUM type that defines what table you want to work with, and add an additional argument that contains an object (i.e a HashTable) that contains name/value pairs needed for your query. This keeps your public interface clean.
i.e:
DBAdaptor.insertEntry(TABLES.table1, valuesHashTable)
instead of:
Table1DBAdaptor.insertEntry(arg1, arg2, arg3, arg4)
Inside the DBAdapter you can create an inner class that handles your implementation details for each type of query.
For the get methods, I'm not sure what the return type is, but you can create a base class and subtype it as needed for the additional return types.
精彩评论