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.
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.
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.
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.