Search code examples
javaandroidobjectdb4o

use of db4o on different class


i'm using db4o in a specific class, and I have this (works OK):

public class xxxxx{
public Db4oHelper db4oHelper = null;
    public void xxxx (Context ctx){
        dbHelper(context);
        //operations...
    }

But now I need use db4o in different class Should I write the method dbHelper(Context ctx) in all class?

I tried program a class with the method and later call the method with object.dbHelper, but doesn't work :(

public class Db4o {
Context context;       
public Db4oHelper db4oHelper = null;
public Db4o(Context ctx){
context=ctx;
}
public Db4oHelper dbHelper(Context ctx) {

                if (db4oHelper == null) {
                    db4oHelper = new Db4oHelper(ctx);
                    db4oHelper.db();
                }
                return db4oHelper;
            }
    }

What can I do?


Solution

  • I'll go with a abstract class that provides the method to set the context and to get the helper. Such a class will be the superclass of all other classes where i need the functionality.

    For instance:

    
    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() {
        }
    }
    

    Using the abstract class is fairly simple, we just extend and we are done.

    
    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
        }
    }
    
    

    Another example of using the helper objects.

    
    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!
    

    Hope it helps.