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.
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();
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?
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).
Joined: Dec 22, 2010
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?
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.
Joined: Dec 22, 2010
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
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,
Joined: Dec 22, 2010
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.
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