Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

B&S 2.3.2 - Data class as a singleton

 
Patrick Jansen
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Roel De Nijs
Sheriff
Posts: 9137
75
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Yucca Nel
Ranch Hand
Posts: 147
IntelliJ IDE Java Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Christian Kindler
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • 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: 9137
    75
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • 0
    • Mark post as helpful
    • send pies
    • 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
     
    I agree. Here's the link: http://aspose.com/file-tools
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic