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.
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
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
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.
Consider Paul's rocket mass heater. |