java - How to implement singleton pattern using ormlite in Android -


i've been testing various orm in android make object-relational-mapping in app i'm developing.

i have made class diagram , have found useful implement singleton pattern in classes.

as singleton pattern states singleton class has make constructor of class private enforce unique instance of class.

considering ormlite page:

2.1.3 adding no-argument-constructor

after have added class , field annotations, need add no-argument constructor @ least package visibility. when object returned query, ormlite constructs object using java reflection , constructor needs called.

how can implement pattern using ormlite?, give example:

the class colectorprincipal represents user of app, since there 1 person @ time using app want make sure there 1 instance of class @ time.

public class colectorprincipal extends persona {      @databasefield(index=true)     private string numerocoleccionactual;     private static colectorprincipal colectorprincipal;     @databasefield(generatedid=true)     private int colectorprincipalid;     @databasefield(foreign=true)     private arraylist<viaje> viajes;     @databasefield(foreign=true)     private arraylist<proyecto> proyectos;     @databasefield     private int tipocapturadatos;      /**      *       * @param usuario      * @param contraseña      */     private colectorprincipal(string usuario, string contraseña){         super(usuario, contraseña);         setusuario(new usuario(usuario, contraseña));     }      /**      *       * @param usuario      * @param contraseña      */     public static colectorprincipal getcolectorprincipal(string usuario, string contraseña){         if (colectorprincipal == null) {             colectorprincipal=new colectorprincipal(usuario, contraseña);         }         return colectorprincipal;     } ...  } 

this class have transient attributes, , persistent.

this class extends class called persona represents person has data names, phone, address ... attributes persistent class person not persistent since want store data both classes in single table.

public class persona {      @databasefield     private string nombres;     @databasefield     private string apellidos;     @databasefield     private string direccion;     @databasefield     private string telefono;     @databasefield     private string institucion;     private usuario usuario;      public persona(){      }     /**      *       * @param apellidos      * @param nombres      */     public persona(string apellidos, string nombres){      }     ... } 

is possible make ormlite instantiate class tru singleton method getcolectorprincipal?

i'm not 100% sure understand requirements here.

i have made class diagram , have found useful implement singleton pattern in classes.

it doesn't make sense have database entities singletons. ormlite needs instantiate instances of colectorprincipal if gets database.

this 1 of singleton classes want make persistent represents user of app.

so have 1 user in entire jvm? don't it.

if want retrieve singleton database, might consider wrapping class. wrapping class enforce singleton initialize it's information database using entity.

i'm not sure helps newer feature of ormlite support objectfactory can set on dao. factory allows control instantiation of object. here javadocs objectfactory.

is possible use ormlite without public constructor?

the constructor not have public. believe package privileges works.

edit:

it sounds looking want 1 user logged in @ time. i'd create instance of userlogin in table when user logs in deleted when user logs out. when user goes login, see if there valid entries in userlogin table.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -