Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Singleton pattern doubt

 
Vivek Hingorani
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read about singleton and tried implementing the same via this piece of code:


Then wrote a main method in the same class and tried testing it:


As its the same class I was able to create a object with new operator. Here both the Objects are different and even equals prints false. Is it not the correct singleton implementation?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49382
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whether you should be using singletons in the first place is doubtful, but …

Where did you find that? It is a pretty poor implementation. And putting your main method in the same class is another bit of dubious design.
You are using double‑checked locking, and everybody knows double‑checked locking doesn’t work. … and they are mistaken; it does work in Java5+.
I think Joshua Bloch (Effective Java™, but I don’t have my copy to hand at present) recommends you implement a singleton like this:-I think I have got that right; please check.

No need to do anything with clone(). If you don’t implement Cloneable, you can’t use clone() anyway. Similarly, don’t implement Serializable.
 
Vivek Hingorani
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agreed double locking doesnt work.. But Whats the prob in having main method in same class?

That is breaking my singleton and hence the doubt.
 
Winston Gutkowski
Bartender
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vivek Hingorani wrote:Is it not the correct singleton implementation?

Not really.

1. SingletonsAreEvil. You may also find this article useful.
2. Lazily instantiated singletons (which appears to be what you're trying to do) are doubly evil.
3. If, in spite of all the evidence, you absolutely feel you must have a singleton:is probably the easiest way to define it.

Winston
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vivek Hingorani wrote:But Whats the prob in having main method in same class?

If your constructor is private then the only way to create an instance of that class is from within the class itself.
In your class there are two methods that create instances of your class
  • getSingleInstance which only allows one instance to be created
  • main which creates multiple instances

  • Therefore if you want to make sure only one instance is created, get rid of your main method.
     
    Naveen Bachu
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This implementation seems to be very poor, we create Singleton class so that no one should be able to create two instances Note: this rule is also applicable for the person who is writing this class

    Here is the example which explains about implementation of Singleton class: Best Way to Create Singleton Class
     
    Junilu Lacar
    Bartender
    Posts: 7480
    50
    Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Naveen Bachu wrote:Here is the example which explains about implementation of Singleton class: Best Way to Create Singleton Class


    That's not a good example. In fact, you should avoid implementing a Singleton that way because it is not thread-safe. See Campbell's and Winston's examples instead.
     
    Naveen Bachu
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This will still work by adding synchronized block
     
    Junilu Lacar
    Bartender
    Posts: 7480
    50
    Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It's still mislabeled as "the best way" at best, falsely advertised at worst. Don't use that method, even with synchronization. There are better ways.
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 49382
    62
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It is surprising how many websites there are which show poor examples of Java code and describe them as the best way.
     
    Naveen Bachu
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It's still mislabeled as "the best way" at best, falsely advertised at worst. Don't use that method, even with synchronization. There are better ways.


    I didn't get what is wrong with the code, except synchronization, Can you guide us to any best example to compare and improve our knowledge?
     
    Junilu Lacar
    Bartender
    Posts: 7480
    50
    Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Naveen Bachu wrote:I didn't get what is wrong with the code, except synchronization, Can you guide us to any best example to compare and improve our knowledge?

    That guidance has already been given. Read through the thread again.

    See also this wikipedia entry on singleton pattern but read it carefully so you know which ones are examples of bad implementations vs which ones are good implementations.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic