*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes The ugly side of Data.getInstance(String file, int magicCookie) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "The ugly side of Data.getInstance(String file, int magicCookie) " Watch "The ugly side of Data.getInstance(String file, int magicCookie) " New topic
Author

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

Jianping Wang
Ranch Hand

Joined: May 29, 2010
Posts: 60
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?



SCJP 6 with 93%
Oracle Database SQL Expert with 98%
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5139
    
  12

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


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Jianping Wang
Ranch Hand

Joined: May 29, 2010
Posts: 60
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
Bartender

Joined: Jul 19, 2004
Posts: 5139
    
  12

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

Joined: Jan 14, 2010
Posts: 44
Hi.

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



BR
Peter
Raf Szczypiorski
Ranch Hand

Joined: Aug 21, 2008
Posts: 383
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

Joined: Jun 14, 2010
Posts: 6
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

Joined: May 29, 2010
Posts: 60
Looks decent!!!
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5139
    
  12

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
 
 
subject: The ugly side of Data.getInstance(String file, int magicCookie)
 
Similar Threads
NX: Multithreaded Testing Problem
Data access class desing
RMI method invocation return value
Data: Mutition/Singletone
URLyBird: about read the records from the database file.