File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes singleton lazy initialization & file name Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "singleton lazy initialization & file name" Watch "singleton lazy initialization & file name" New topic
Author

singleton lazy initialization & file name

David Pantale
Ranch Hand

Joined: Mar 16, 2010
Posts: 32
Hi!

I (like so many others) am using the singleton pattern. I use double-checked lazy initialization with the volatile keyword when creating my instance. I also pass in the file name (selected by the user) to the static getInstance method that creates my Data object. My questions are 1) has anyone heard about problems in the grading with using double-checked lazy initialization (at one time it was broken), and 2) any problems with passing the db file name into the getInstance method, one person I spoke to thought it was not proper.

Thanks
Dave
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5226
    
  12

Hi Dave,

1/ I believe it was fixed with the new memory model as of JDK 5.0, more info can be found here.

2/ My good friend Roberto also used 2 getInstance-methods, one with the database file name, one with no parameters at all. And he passed, so it's an accepted solution. In my opinion it's not proper, so I used another approach: I created a custom interface (extending the given interface), added 2 extra methods (one for initializing and one for "destroying") to my custom interface. The initialize-method has one parameter, the database file name. This method must be invoked first, before any other method invocation (otherwise an IllegalStateException will be thrown). And another nice advantage of this approach is the easy declaration and initialization of the singleton instance, just private static final MyOwnInterface SINGLETON = new Data();

Kind regards,
Roel


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

Joined: Mar 16, 2010
Posts: 32
Thanks for replying so promptly Roel. I'm curious though, why do you think passing in the file name to the constructor is improper?

Dave
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5226
    
  12

Hi Dave,

I could not explain it better than this opinion.

But like I said in my previous post: just my opinion, you can easily add the parameter to the getInstance-method and pass this certification.

Kind regards,
Roel
David Pantale
Ranch Hand

Joined: Mar 16, 2010
Posts: 32
The writer is a man of strong opinions - and very clear, too. Thanks, Roel.

Dave
Kenneth Logan
Greenhorn

Joined: Dec 22, 2010
Posts: 21
And another nice advantage of this approach is the easy declaration and initialization of the singleton instance, just private static final MyOwnInterface SINGLETON = new Data();


When getInstance() is called, should it return the "MyOwnInterface" object or "Data" object? I have not created a MyOwnInterface. Instead, I just create: private static final Data data = new Data(); The getInstance() returns Data so the init() and shutdown() methods can be called (these two methods, however, are not in the DB interface provided by Sun). So I'm wondering if I am not doing this properly because I'm no longer programming to the "DB" interface when I do this. Thoughts?

Thanks,
Kenneth
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5226
    
  12

Hi Kenneth,

You should always program against interfaces, because you should be able to switch different implementations of an interface without any problem. In your solution you'll get some compiler errors, because init and shutdown methods are not defined in the new DB implementation (which uses a RDBMS instead of a file for example).

Kind regards,
Roel
Kenneth Logan
Greenhorn

Joined: Dec 22, 2010
Posts: 21
Roel,

The problem is I'm already saying "Data.getInstance()" so what's the harm in saying "Data.init(String filename)" (a static method) before the "Data.getInstance()" is called?

Kenneth
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5226
    
  12

First of all: it was not obvious at all that these 2 methods were static ones. I have similar methods, but they are part of my own custom interface. If you don't tell it are static ones, I can't guess it.

Secondly I think having a static init and shutdown methods is not a good coding practice at all. Having a singleton you would expect that the init-method will initialize the single instance and not some class variables which you'll be using in your singleton. But of course it will work without a problem.
Kenneth Logan
Greenhorn

Joined: Dec 22, 2010
Posts: 21
I was debating whether to make the init/shutdown methods static or not. In my first question, I was assuming not to make them static. In my second question, I was wondering if it was okay to make them static (rather than having to customize my own interface).

Customized Interface approach): I need to add two methods: init / shutdown- non-static of course. In the Data class, all of the DB-inherited methods will need to check if the init method was called first (throw a runtime exception if not). This is because the create/update/delete etc cannot be called without a database file initialized first.

Static init/shutdown methods approach): Not good coding practice, but will still work without a problem.

So now I am debating which is worse:
1) bad coding practice of static init/shutdown methods
vs.
2) having an "if init method not defined, then throw runtimeException" check for almost all of the methods defined in the Data class.


Thanks for the help,
Kenneth
Kenneth Logan
Greenhorn

Joined: Dec 22, 2010
Posts: 21
eeek, after trying to write the shutdown method as a static method, I ran into a bunch of hairy problems. I think the other way will actually be smoother in the long run.

Thanks Roel!
Kenneth
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5226
    
  12

Kenneth Logan wrote:2) having an "if init method not defined, then throw runtimeException" check for almost all of the methods defined in the Data class.

As you already know I followed this approach, throwing an IllegalStateException when someone tried to invoke any method on a not-initialized Data class. It's simple and easy code, 1 extra line of code in each method and the check itself is also pretty straight-forward. Makes the choice very simple: 2 it will be
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: singleton lazy initialization & file name