i was reading a book head first design pattern, actually read that book many times, an excellent book. Sharing my details idea singleton design patter.
class diagram of Singleton design pattern
a singleton class is look like that
public class Singleton
private static Singleton UniqueInstance; //variable to hold one instance of the class Singleton
public static Singleton getInstance()
here the Singleton class constructor is private and it has only one static method to get it, that ensures that this class has only one instance, and provide a global point to access it.
Now think for multi threading…
what will happen??? suppose you have 2 threads, so 2 different instance of the object will be created.
WHATS THE SOLUTION ??? (more…)