• Post Reply Bookmark Topic Watch Topic
  • New Topic

Best way to implement singleton  RSS feed

 
saran harini
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I browsed I came to know two ways of implementing singleton.. I dont know which is best..
I am implementing this to load resource bundle only once for my jvm using constructor to getBundle.
Or to use static method to load..
and I am calling this as I wanted to knw which option is better and why.. and in the second case how can i call the getMessage() method?
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saran

Your examples do not look quite right.

In the first one, you are always creating a new instance of bundle when getInstance() is invoked.
In the second example, instance is never initialised so getInstance() will always return null.
The singleton pattern should be implemented along the following lines:
BTW, standard Java naming would dictate that bundle should be called Bundle.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and in the second case how can i call the getMessage() method?

Something like this:
Like I stated above, you really should rename bundle to Bundle. It then becomes more apparent that in the call Bundle.getMessage(messageId), getMessage is a static method.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I mentioned in my reply to your other thread, if you are using the static block to initialize messages, you do not need the instance at all, as your getMessage is static
 
saran harini
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok fine I could get whatever corrections you told me.. and again to narrow down my question - where can getBundle() be called either inside a constructor or in static block...
To be clear I am using this resource bundle for I18N of runtime messages.. So it can arise in any of my packages or classes.. so everytime i need this to be logged in the same log file..
or
which one will be a better option based upon all constraints like time, resource utilization and so on..
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would go with the static block
 
saran harini
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK.. Thanks
 
saran harini
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
will it be able to handle excptions in this static block somethinglike

 
Campbell Ritchie
Marshal
Posts: 56587
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Disagree.
This is the correct way to implement a Singleton, and has been for nearly ten years:-And why do you want a singleton? Ten years ago they were the best thing since sliced bread, and now people regard them as almost totally evil.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The whole thing is somewhat academic in this case as by default ResourceBundle objects created by calling getBundle() are cached so you can call getBundle() as many times as you like and provided you supply the same base name and locale you will get the same ResourceBundle object back anyway so there is no need for a singleton to store it in.
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you go back to the original code, the only purpose of the instance variable which referred to the singleton object was so that a static method could be called on that singleton. Which means that it's totally unnecessary, as we already saw. That's one of the main problems with singletons, as far as I can see: they're like a drug. When you feel you have the need for a singleton, all of your mental energies are focused on getting it and you don't pay attention to basic needs.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Last week I was working on an Alarmable job that would run a particular scheduled task on a particular server. Most of the logic was already existing-- I had to only implement the run the job on a particular server part. But the fact that the class also managed its own one mutable instance created using the official singleton pattern overly complicated things. I really felt the need to move the instance management to another manager class.

A class that does something and also manages its own instances is really, really bad -- there are many other problems also with that design but I feel ( even more strongly now ) that a class should have a well defined one responsibility only.

Edit : And since we can always have these manager classes ( or factories or whatever ) do the instance management ( if needed ), I can't think of a use case where a class should do something say A and also manage its own instance.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell

I believe Joshua Bloch introduced the idea of using an enum for a singleton in 2008, I don't think it has been around as long as you claim. As much as I see the benefit an enum brings to the pattern, I don't particularly like the one value enum style. Call me old fashioned but I still view an enum as a restricted set of two or more values.
 
Campbell Ritchie
Marshal
Posts: 56587
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The enum has been around since Java5 in September 2004. The old Java5 edition of the Java Language Specification (page 249) says
there is only one instance of each enum constant
…, so it was known then that enum constants are singletons, if you include the version of the singleton which has multiple instances.

Agree the one‑instance version is hardly brilliant, and since singletons have changed from a pattern to an anti‑pattern, I agree you shouldn't use it.
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:...since singletons have changed from a pattern to an anti‑pattern...

Well, you certainly have my attention now. What's so bad about singletons?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That discussion probably goes beyond what is appropriate for the beginner's forum, so I'll just point to Singleton pattern - the "References" section points to some articles on that.
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:That discussion probably goes beyond what is appropriate for the beginner's forum, so I'll just point to Singleton pattern - the "References" section points to some articles on that.

Ah, yes. I was about to post that I had looked at that page, so no need to bother with answers here. The arguments in the cited articles are fairly forceful (except the one in Chinese, which may be forceful and it may not, but I can't read Chinese), but persistently dogmatic. Good to know what they have to say, though.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the code posted in the original post, I second what Tony has said. I don't understand why is the 'bundle' ( ok, yes it should be Bundle ) class required.

But if we are discussing Singletons, a few months back, somebody had posted a question on double checked locking here. And then the OP asked a question on what was the best way to implement a Singleton and we discussed about a few ways ( good, old days .. ). The reason I mention this thread is I think it really has good advice.

Also here Winston points to a very valuable link that explains why 'Singletons' ( the ones created using THE Singleton pattern -- I'm not talking about a singleton here which means that you have ensured you invoked the constructor only once and that you have only one object of a type that does something ) are evil. I highly recommend the two links suggested by Winston.
 
Anindya Roy
Ranch Hand
Posts: 76
 
Campbell Ritchie
Marshal
Posts: 56587
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't like that tutorial. It doesn't supply enough explanation about the examples, and there is something peculiar about having that getInstance method in the enum.
 
hector nuñez
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have used it to create my database connection to mysql and its a very efficient way to do it.
i use the following code to do it.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch Hector!

I want to warn you about a couple of issues with your code.

First of all, all your variables are package-private, and as such can be accessed outside of your class. you should make them private. You also shouldn't hardcode usernames and passwords in your code. Hardcoding the connection URL is probably also a bit iffy.

I think it's questionable to keep a connection alive in a static variable. If reopening the connection all the time is expensive, you should use a connection pool that handles this for you. Inject these through a repository constructor. Here's an example in a student enrollment system:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!