• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

The ugly side of Data.getInstance(String file, int magicCookie)

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Data is a singleton in my project. I found that the Data.getInstance(String file, int magicCookie) is ugly because every time it is called, it creates a new RandomAccessFile and a new lockMap. Singleton Pattern in Books has no parameters in getInstance so it looks perfect. But here we have to pass on a list of parameters.

If I want a singleton created with parameters only once, and use Data.getInstance() only. How to achieve that?
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could enforce a call to getInstance(param1, param2,..., paramN) first (which creates your singleton). And then you can just use getInstance() for all other calls which just returns the singleton. If someone calls the no-arg getInstance method before the one with the parameters, you can simply throw an IllegalStateException
 
Jianping Wang
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So the meaning of getInstance is changed. It more like createInstance(param, param), eh, but this is more like a factory method. If I write a static method
then if getInstance() throw IllegalStateException, the message will be "no database mounted"

The state of Data can be "idle", "mounted". Still thinking, no conclusion.

Your solution is cool. But there is a dependence between those two getInstance methods. And the real meaning of getInstance(param, param) is opening database file, preparing lockMap and then return the instance.

 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jianping,

I know Roberto used such an approach (and passed). My approach consists of 1 no-arg getInstance() method, and I added 2 methods in my own interface (which extends Sun's required interface): a start (to mount the database) and a stop (to unmount the database). The start method has some arguments (like the path of the database file). When you invoke another method (e.g. read) before calling start, you'll get an IllegalStateException.

Kind regards,
Roel
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.

Or you could just initialize the RandomAccessFile if instance == null.



BR
Peter
 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My create method also takes a parameter, a File. I canonicalize that file (which makes the file unique per JVM) and check if it was used before. If so, I return the Data associated to it; if not, I create a new one and return it.
Each new Data object is put to a map: canonicalized File -> Data when created for the first time. This means that you can use as many databases as you want in your code (you never know how and by whom it is going to be used, and the design is quite flexible), but if you point to the same file (no matter if the paths are absolute, relative, contain ../whatever/.. magic - canonicalization takes care of that) you get the same Data instance, which works nicely as locking works. If you close the database, it deregisters itself from the already created Data cache.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello

Jianping Wang wrote:Data is a singleton in my project. I found that the Data.getInstance(String file, int magicCookie) is ugly because every time it is called, it creates a new RandomAccessFile and a new lockMap. Singleton Pattern in Books has no parameters in getInstance so it looks perfect. But here we have to pass on a list of parameters.

If I want a singleton created with parameters only once, and use Data.getInstance() only. How to achieve that?



I used a singleton in my solution also, but getInstance() just returns the unique instance which at first is a un-opened database. The call to create and open it is something like this:

MyClass.getInstance().startup("filename", 123);

Any database operations performed without startup() being called first will result in a database exception. I have also a getStatus() method.

Not sure if from a "pattern" point of view is ideal but works and it seems simply enough to me... any coments welcome!

Luis
 
Jianping Wang
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks decent!!!
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Luis Cabral wrote:Not sure if from a "pattern" point of view is ideal but works and it seems simply enough to me... any coments welcome!


I used exactly the same approach
reply
    Bookmark Topic Watch Topic
  • New Topic