Java db4o在不同类上的使用
我在一个特定的类中使用db4o,我有一个(工作正常): 但是现在我需要在不同的类中使用db4o,我应该在所有类中编写方法dbHelper(contextctx)吗 我尝试使用该方法编程一个类,然后使用object.dbHelper调用该方法,但不起作用:(Java db4o在不同类上的使用,java,android,object,db4o,Java,Android,Object,Db4o,我在一个特定的类中使用db4o,我有一个(工作正常): 但是现在我需要在不同的类中使用db4o,我应该在所有类中编写方法dbHelper(contextctx)吗 我尝试使用该方法编程一个类,然后使用object.dbHelper调用该方法,但不起作用:( 我能做什么?我将使用一个抽象类,它提供了设置上下文和获取帮助程序的方法。这样一个类将是我需要该功能的所有其他类的超类 例如: public abstract class PersistenceObject { private Co
我能做什么?我将使用一个抽象类,它提供了设置上下文和获取帮助程序的方法。这样一个类将是我需要该功能的所有其他类的超类 例如:
public abstract class PersistenceObject {
private Context context;
private volatile Db4oHelper helper;
public PersistenceObject() { }
public PersistenceObject(Context context) {
this.context = context;
}
public Context getContext() {
this.getContext();
}
public final void setContext(Context context) {
if (this.context != context) {
if (this.context == null) {
onContextSet(context);
} else if(context == null) {
onContextUnset(context);
} else {
// Should you allow the context to change?
// Maybe the object is dirty, if so what to do?
onContextChanged(context, this.context);
}
this.context = context;
}
}
// we can call initialize on objects that extends this class
// this initializes the context and also call the overloaded
// initialize method to allow the classes to provided what
// need to be done after we setup the context
public final void initialize(Context context) {
this.setContext(context);
this.initialize();
}
private void onContextSet(Context context) {
// ...
}
private void onContextUnset(Context context) {
// lets clean the helper when we set the context to null
this.helper == null;
}
private void onContextChanged(Context newContext, Context oldContext) {
// Lets assume we can change the context
// Since we change the context lets set the helper to null,
// so in the next call we get a new helper with the right context
this.helper == null;
}
protected final Db4oHelper getDb4oHelper() {
Db4oHelper helper = this.helper;
if (helper == null) {
synchronized(this) {
helper = this.helper;
if (helper == null) {
helper = this.helper = new Db4oHelper(context);
helper.db();
}
}
}
return helper;
}
public void initialize() {
}
}
使用抽象类相当简单,我们只是扩展了它,就完成了
public class Category extends PersistenceObject {
private String name;
public Category() {
}
....
}
使用辅助对象的另一个示例
public final class PersistenceObjectHelper {
private PersistenceObjectHelper() { }
public static void doOPERATION(PersistenceObject object) {
context = object.getContext();
if (context == null) {
// either the object was not initialized or the context was set to null
// which basically means that we dont have a way to create the helper,
// lets fail
throw new IllegalArgumentException("Unable to create the DB4O due to lack of context");
}
DB4oHelper helper = object.getDB4oHelper();
// use the helper and the object to do whatever we need
}
}
Category category = new Category();
PersistenceObjectHelper.doOPERATION(category); // fails due to lack of context
Category category = new Category();
category.initialize(context);
PersistenceObjectHelper.doOPERATION(category); // works!
Category category = new Category();
category.setContext(context);
PersistenceObjectHelper.doOPERATION(category); // works!
希望能有所帮助。哦..我有个问题..我需要在一个扩展BroadcastReceiver的类中使用db4o:(和..我不能扩展两个类..您将模型与服务层混合在一起,我想,您可以使用“服务”类来扩展BroadcastReceiver,并且在该实例中有您想要列出的实体。
public class Category extends PersistenceObject {
private String name;
public Category() {
}
....
}
public class Product extends PersistenceObject {
private Category category;
public Product() {
}
// ...
public void initialize() {
Db4oHelper helper = getDb4oHelper();
// use the helper to perform whatever operations it needs
}
}
public final class PersistenceObjectHelper {
private PersistenceObjectHelper() { }
public static void doOPERATION(PersistenceObject object) {
context = object.getContext();
if (context == null) {
// either the object was not initialized or the context was set to null
// which basically means that we dont have a way to create the helper,
// lets fail
throw new IllegalArgumentException("Unable to create the DB4O due to lack of context");
}
DB4oHelper helper = object.getDB4oHelper();
// use the helper and the object to do whatever we need
}
}
Category category = new Category();
PersistenceObjectHelper.doOPERATION(category); // fails due to lack of context
Category category = new Category();
category.initialize(context);
PersistenceObjectHelper.doOPERATION(category); // works!
Category category = new Category();
category.setContext(context);
PersistenceObjectHelper.doOPERATION(category); // works!