我真的很喜欢活动记录的想法,我将实现以下设计:所有具体模型都扩展抽象模型,它具有基本的 CRUD 操作。
这是模型上的示例保存功能:
public void save(){
try {
getDao().createOrUpdate(this);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
这是getDao():
private static Dao<Model, Integer> getDao(Context context){
Dao<Model, Integer> result = null;
DatabaseHelper dbHelper = new DatabaseHelper(context);
try {
result = dbHelper.getDao(Model.class);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
如您所见,我有 Model.class。除了将 Class 传递给 getDao 函数之外,还有其他选项或模式可以实现以下设计吗?