/** Singleton declaration of Data
* class constructor
*/
private Data(){
}
/**
* method will return singleton instance of Data
* @return Data
*/
public static synchronized Data getInstance(){
if (uniqueData == null){
uniqueData = new Data();
}
return uniqueData;
}
SCJP, SCWCD, SCBCD, SCJD, BB Java2 and JSP1.1
SCJP, SCJD, SCWCD, OCPJBCD
SCJP, SCWCD, SCBCD, SCJD, BB Java2 and JSP1.1
Originally posted by Jonathan Moore:
Hi Alan,
Just to post my 2 cents, is there a definite reason why your Data class is a singleton?
I had my Data class as a singleton originally, but then it occurred to me that this may be limiting my design. The database file effectively represents a table, but what if in the future there were 2 or more database files (with different datasets) that could be accessed? The DB interface is generic and so the Data class can also be generic, but you would need more than one instance to access more than one database file, which can't be done if your class is singleton.
I decided not to make the class a singleton, and to pass the database file name into the Data constructor. When I open the file I attempt to lock it using FileChannel.tryLock(), and throw an exception if a lock already exists. This means I can have multiple Data instances, but only one for any given database file. Maybe I'm over-complicating the issue, but thought you may appreciate a different perspective.
Sorry if this is slightly off topic.
Cheers
Jon
F is for finger. Can you stick your finger in your nose? Doesn't that feel nice? Now try this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|