Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Singleton pattern doubt

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 76486
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
     
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
     
    Sheriff
    Posts: 17153
    298
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    This will still work by adding synchronized block
     
    Junilu Lacar
    Sheriff
    Posts: 17153
    298
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Marshal
    Posts: 76486
    366
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Sheriff
    Posts: 17153
    298
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Look ma! I'm selling my stuff!
    Garden Master Course kickstarter
    https://coderanch.com/t/754577/Garden-Master-kickstarter
    reply
      Bookmark Topic Watch Topic
    • New Topic