aspose file tools*
The moose likes Java in General and the fly likes singleton pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "singleton pattern" Watch "singleton pattern" New topic
Author

singleton pattern

nancy andrew
Ranch Hand

Joined: Jan 19, 2010
Posts: 30
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??
vijin das
Ranch Hand

Joined: Jun 07, 2010
Posts: 129

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


VIJINDAS
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
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.


Joanne
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575



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

Joined: Jun 07, 2010
Posts: 129

sharing wiki link which says more about it..
check wiki
Avishkar Nikale
Ranch Hand

Joined: Aug 06, 2010
Posts: 173
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.


Regards,
Avishkar Nikale
Virendrasinh Gohil
Ranch Hand

Joined: Jun 09, 2004
Posts: 46
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

Joined: Jan 28, 2008
Posts: 5575

Yes, understand . thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: singleton pattern
 
Similar Threads
review lock/unlock
Static reference for object
Singleton class confusion
Understanding singleton pattern
Singleton Class