• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

B&S 2.3.2 - Data class as a singleton

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

I decided to implement the Data class in my B&S 2.3.2 asssignment as a singleton. So I will have only one instance of Data at any given time. In almost everywhere I saw examples in which getInstance method have no parameters. Because I need to initialize the databaseLocation field of my Data class I need to define getInstance method with one String parameter. Still am I conform to singleton pattern if my getInstance method has a parameter?
My code is:

Many thanks.
 
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 Patrick,

I don't think there is anything wrong with the adding of a parameter to the getInstance(). I know the great and wise Roberto Perillo had a similar approach, but he created 2 getInstance() methods: one with a parameter (must be called first), one without a parameter (for all other calls).

I also used the singleton pattern and had just 1 getInstance() method without any parameters. I can hear you think: "but how did you pass the database location" Well I extended the sun interface and added a method to initialize my Data-class and that method had the parameter with the database location. This init-method must be called first, so before any other call to the Data instance was made. If you called the read-method (or any other method) before the init-method, you got an IllegalStateException.

Kind regards,
Roel
 
Patrick Jansen
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Roel for your reply. Now is clear for me how can I use the getInstance method without any parameters.
Now I am working on the database layer and I hope to finish my assignment until January 2010 (my deadline). After reading some stuff(Head First OOAD, Head First Design Patterns, SCJD Study Guide by Kathy SierraandBert Bates, Sun tutorials for Threads, RMI and the Adrew book) I decided to start designing my assignment. I have no experience in applications design so I am still confused, but finally I decided to start the job.
Thanks in advance to all ranchers that will guide me.
 
Ranch Hand
Posts: 147
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A singleton is perfectly fine as long as you make sure that each DB implementation gets its own singleton. You are only working with a small database and having a singleton facade design IOW a single class that does all the DB implementaion (data accessing and record locking) is a good choice. I would like to add that this DB (CRUDL) interface that sun gave you is a very reusable one and widely used.

May I suggest the outdated but still very useful Java RMI that will help you with RMI if you choose that as well as Java cookbook that provides awesome utility methods that you may find of use.
 
Patrick Jansen
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Yucca,
I don't understand exactly what do you mean with the following:

Yucca Nel wrote:A singleton is perfectly fine as long as you make sure that each DB implementation gets its own singleton.


and

Yucca Nel wrote:
You are only working with a small database and having a singleton facade design IOW a single class that does all the DB implementaion (data accessing and record locking) is a good choice.


You mean that if my Data class is a singleton I have to implement data accessing and record locking directly in the methods of Data class and not use composition having two classes for data accessing and record locking?
BTW: in a class that is a singleton, make sense to declare its fields as statics if we have only one instance of that class? I think no, only when that fields is used in static methods.
Thanks.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roel De Nijs wrote:Hi Patrick,
I also used the singleton pattern and had just 1 getInstance() method without any parameters. I can hear you think: "but how did you pass the database location" Well I extended the sun interface and added a method to initialize my Data-class and that method had the parameter with the database location. This init-method must be called first, so before any other call to the Data instance was made. If you called the read-method (or any other method) before the init-method, you got an IllegalStateException.



I also use the singleton pattern and a init method. But I am not sure what to do if the init method is called more than once.

  • ignore further calls to the init method after the data class instance is initialized, or
  • throw an IllegalStateException if the init method is called and the data class instance is already initialized, or
  • throw an IllegalStateException if the init method is called and the data class instance is already initialized and if the given parameter is different from the parameter used to initialize the singleton

  •  
    Roel De Nijs
    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 Christian,

    I think that's just up to you and just make it clear in the javadoc (and maybe in your choices.txt).

    What i did was: the first init-call inits, all following init-calls do nothing. I had also a stop-method, so if you wanted to restart the data class (with maybe another file-path) you first had to call stop-method and then invoke init again.

    Kind regards,
    Roel
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic