File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Jianping Wang
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Roel De Nijs
Sheriff
Posts: 9188
77
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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: 9188
77
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Peter Aarum
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.

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



BR
Peter
 
Raf Szczypiorski
Ranch Hand
Posts: 383
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Luis Cabral
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks decent!!!
 
Roel De Nijs
Sheriff
Posts: 9188
77
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic