• 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

Issues in Singleton Design Pattern

 
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Siddharth Bhargava
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do I write my own ClassLoader if I want to use it for loading a Singleton class. Please give some code example.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic