• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

singleton pattern

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was recently asked this question: Create a singleton class which returns same object of that class itself everytime.
I know singleton class looks like this:



But i should not use a new operator like instance = new TestSingleton(); In short, my program should return same instance of same class everytime. Any pointers??
 
Ranch Hand
Posts: 129
Firefox Browser Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a clone() method defined in the java.lang.Object class which TestSingleton is inherited from.
By default, the clone() method is marked as protected, but if your TestSingleton extends another class
that does support cloning, it is possible to violate the design principles of the singleton.

So,we must add a clone() method of our own, and throw a CloneNotSupportedException ! for 100% certain that TestSingleton is a singleton
so we can modify it like this

 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

nancy andrew wrote:I was recently asked this question: Create a singleton class which returns same object of that class itself everytime.
I know singleton class looks like this:



But i should not use a new operator like instance = new TestSingleton(); In short, my program should return same instance of same class everytime. Any pointers??



You have to use the new operator once to create the single instance that you are going to return every time for all future calls to getInstance.
Your code is generally correct although it could have problems in a multithreaded environment. You need to either make your getInstance method synchronized or initialise the instance variable at the same time as you declare it.
Search these forums for examples of how it should be done.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


additionally, *use double-check technique* instead of adding a synchronized key word infornt of getInstance method's modifier. I believe below code is bit better in terms of performance.



hth
 
vijin das
Ranch Hand
Posts: 129
Firefox Browser Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sharing wiki link which says more about it..
check wiki
 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nancy,

There also are environments where mutiple class loaders can lead to more than one instance.

Thread safety during creation is also a conern.

I would also suggest you to learn where you can use "volatile" & "ThreadLocal" as
not every requirement completely maps to a singleton pattern to be implemented.

The link provided by Vijin is very helpful & do check Bill Pugh's solution provided on the wiki.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Seetharaman Venkatasamy wrote:....
....
additionally, *use double-check technique* instead of adding a synchronized key word infornt of getInstance method's modifier. ....
....
....hth



Seetharaman, double-check technique is bad. Specially when it is used with any version upto Java 1.4. It gives you false feeling that code is thread safe but it is not. However 1.5 modified the way objects are created and hence made double-check safe. But if you don't want your code version dependent, use method mentioned by "Joanne Neal". They are in a way fool-proof (of couse they are NOT 100% as reflection and manipulation with classloader instance allows multiple instances of singleton).

Please read this. This explain the exact problem and it's solution: http://www.javamex.com/tutorials/double_checked_locking.shtml
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, understand . thanks
reply
    Bookmark Topic Watch Topic
  • New Topic