• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

Server Data class as Singleton?

 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 221
Scala Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Your mother is a hamster and your father smells of tiny ads!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic