*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes B&S 2.3.2 - Data class as a singleton Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "B&S 2.3.2 - Data class as a singleton" Watch "B&S 2.3.2 - Data class as a singleton" New topic
Author

B&S 2.3.2 - Data class as a singleton

Patrick Jansen
Greenhorn

Joined: Jan 30, 2009
Posts: 10
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.


SCJP 5.0
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

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


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Patrick Jansen
Greenhorn

Joined: Jan 30, 2009
Posts: 10
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.
Yucca Nel
Ranch Hand

Joined: Nov 20, 2008
Posts: 147

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.


SCJP 6.0, SCJD (400/400), SCBCD for JEE 5, SCWCD 1.4 I do videos for development at
http://www.youtube.com/user/thejartender?feature=mhee
I am probably the only developer ever to have had an orange sized brain tumor in my brain while learning development!!
Patrick Jansen
Greenhorn

Joined: Jan 30, 2009
Posts: 10
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.
Christian Kindler
Greenhorn

Joined: Apr 16, 2009
Posts: 18
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
    Bartender

    Joined: Jul 19, 2004
    Posts: 5126
        
      12

    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
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: B&S 2.3.2 - Data class as a singleton
     
    Similar Threads
    Is accessing static class variables from non-static methods bad practice?
    Making the Data Class a Singleton & Thread Safe
    Data class and server code
    Combination of Factory and Singleton ?
    NX: exposing the MetaData to the client