Meaningless Drivel is fun!*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Issues in Singleton Design Pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Issues in Singleton Design Pattern" Watch "Issues in Singleton Design Pattern" New topic
Author

Issues in Singleton Design Pattern

Siddharth Bhargava
Ranch Hand

Joined: Feb 23, 2007
Posts: 257
Hi All,
After going through many articles and books I have found out that there are mainly 4 issues with the Singleton Pattern. I have doubts in 3 issues. Please help me in clearing my concepts. Any Java Code example would be helpful.

1. Multithreading Issue - Why we use "double-checked locking" in Singleton method. Please do explain with a code example.

2. Serialization Issue - It says, that if you deserialize a Singleton object more than once, then you will have multiple Singleton instances. Firstly, why would anyone deserialize it twice. And Secondly, How can we prevent this.

3. ClassLoading Issue - It says that it is possible to have multiple Singleton instances if classes are loaded by different classloaders. How do we prevent this.

4. Cloning - This is quite clear to some extent. A Java code example would help.

Please do help me in clearing my concepts. Thanks in advance.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874

1. Multithreading Issue - Why we use "double-checked locking" in Singleton method. Please do explain with a code example.


Here you can think of any frameworks which spawn multiple threads behind the scene, for example Servlet. In this case your singleton will be shared among different threads and need to be threadsafe. Moreover, a singleton object which doesn't posses any state, need not to be threadsafe.


2. Serialization Issue - It says, that if you deserialize a Singleton object more than once, then you will have multiple Singleton instances. Firstly, why would anyone deserialize it twice. And Secondly, How can we prevent this.


Deserialization of a singleton might result in more then one instance, which is clearly wrong for a singleton. Because it completely kills the purpose. For this you consider providing readResolve() method to preserve single instance.


3. ClassLoading Issue - It says that it is possible to have multiple Singleton instances if classes are loaded by different classloaders. How do we prevent this.


I believe, you will probably expereince LinkageError in this case, if its in the same classloading hierarchy. Even if you get this scenario, not a single instance will be compatible with the other, means you can't even cast one to the other.


4. Cloning - This is quite clear to some extent. A Java code example would help.


Override a clone method carefully, would do the trick. Better, in some cases, override the clone method and throw a NotSupportedException.

Cheers.

For more, I would highly recommend Effective Java.
[ August 24, 2008: Message edited by: Adeel Ansari ]
Siddharth Bhargava
Ranch Hand

Joined: Feb 23, 2007
Posts: 257
Please could you explain me ClassLoading and Serialization issue through a Code example. It will help me in clearing my concepts.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Actually, each deserialization of a serialized in instance will result in a new instance. For that you must consider to provide a readResolve() method. For example,

Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
For your third question, please read how classloader works. You can search Yahoo for that, there are plenty of resources discussing that.

Cheers.
Siddharth Bhargava
Ranch Hand

Joined: Feb 23, 2007
Posts: 257
Hi,


each deserialization of a serialized in instance will result in a new instance.


Does that mean that for every serialized instance if we de-serialize n number of times then we will have n instances. ?

Also what happens to a Singleton in a cluster.

Thanks and Regards,
Siddharth Bhargava.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874

Does that mean that for every serialized instance if we de-serialize n number of times then we will have n instances. ?


Yes, unless you already considered this while coding.


Also what happens to a Singleton in a cluster.


Single instance per JVM, most likely. For more have a look here.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
There are also two design issues with Singleton:

- the global access to the single instance leads to similar coupling problems as with global variables, and

- the knowledge that there is exactly one instance of a class in the system most often shouldn't be the responsibility of the class in question.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Siddharth Bhargava
Ranch Hand

Joined: Feb 23, 2007
Posts: 257
Hi,


There are also two design issues with Singleton:

- the global access to the single instance leads to similar coupling problems as with global variables, and

- the knowledge that there is exactly one instance of a class in the system most often shouldn't be the responsibility of the class in question.


I didn't understand the point. Please explain in more detail.

Thanks
Siddharth
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
The problem with the global access is that all clients know where to get the instance from. Which means that they are harder to reuse in different contexts (for example where the object actually shouldn't be a singleton). This often shows already when you want to unit test the client - mocking the singleton often is quite painfull.

The other problem is that the "singletonness" often is not a property of the class itself, but just a property of the system the class is used in, under the current requirements.

Do you have an example of a where you would use a Singleton? That would make it much easier to discuss the drawbacks, design-wise.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by Ilja Preuss:
The other problem is that the "singletonness" often is not a property of the class itself, but just a property of the system the class is used in, under the current requirements.


Very well said. I have never thought like this before. Thanks, indeed.
Siddharth Bhargava
Ranch Hand

Joined: Feb 23, 2007
Posts: 257
How do I write my own ClassLoader if I want to use it for loading a Singleton class. Please give some code example.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Issues in Singleton Design Pattern
 
Similar Threads
Collection of components
Core Java/JSP/SERVLETS/DESIGN PATTERNS Queries
Data class Singleton question
About Singleton Pattern
B&S Database Design Structure