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 Server Data class as Singleton? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Server Data class as Singleton?" Watch "Server Data class as Singleton?" New topic
Author

Server Data class as Singleton?

Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Posts: 206
Hi Fellow Bloggers,

I'm working on the implementation of the Bodgitt & Scarper assignment. The assignment provides an Interface called DBAccess that must be implemented by the Data class. There isn't any specification requirement prohibiting me from implementing the Data class as a Singleton, but if I implement the Data class as a Singleton, then the class constructor will be "private." This would prevent any automated test programs, used by the SCJD test evaluators, from externally instantiating my Data class in order to test it.

I know that I must explain my choice in the Choices.txt document before submitting my assignment.

Is there any bona fide reason that you know of that prevents me from implementing the Data class as a Singleton?

Thanks,
Harry
Carlos Morillo
Ranch Hand

Joined: Jun 06, 2009
Posts: 221

Hi Harry,


There are a couple of folks like Roberto Perillo and Roel De Nijs who got pretty good scores if not perfect
who used a Singleton. Look at this thread.


Hope this helps,


Carlos.


SCSA, OCA, SCJP 5.0, SCJD http://www.linkedin.com/in/carlosamorillo
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5121
    
  12

Hi Harry,

Like Carlos pointed already out I used a singleton and I got a pretty good score But I know your fear: I was also afraid of the "automatic failure", so when Roberto told me he used a singleton Data class too I was convinced that such an approach was just fine.

Harry Henriques wrote:Is there any bona fide reason that you know of that prevents me from implementing the Data class as a Singleton?
I only have positive experiences with that approach: it keeps your code very easy and simple (certainly if you combine it with just-synchronize-every-method). I don't have any regret doing it like that and if I had to do it again, I would opt again for the Singleton.

Good luck!
Kind regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2229
    
    7

I used a sigleton for my Data class too. The primary logic for me anyway is to think the data file is like a database. It gets "shared" for network use so a singleton will make every users access the same instance.

K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Andriy Pererva
Ranch Hand

Joined: Jul 19, 2009
Posts: 73
Just to be on the safe side it would be reasonable to use Singleton not directly, but in combination with DAOFactory.
This approach gives you freedom to change an implementation of Data class instantiation without changes in clients code.


SCJP 6.0(95%), SCWCD 5(94%), SCJD (working on B&S v.2.3.1)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Server Data class as Singleton?
 
Similar Threads
NX: Passing DB file to Data Class
B&S: Data access class
Take this Testing Tool for Data Class
Locking w/o cookies issues - Again!
DB Interface (URLyBird 1.1.2)