As developers, we all have heard of Singletons. It would not be an exaggeration to state that it is the most popular design pattern. Having said that, what exactly is a Singleton?
A class that can only be instantiated only once is referred to as a Singleton.
In order to design a Singleton class, the following rules should be adopted:
Example 1:
public final class MySingleton implements Cloneable {
private static volatile MySingleton instance = null;
private MySingleton() {
}
public static synchronized MySingleton getInstance() {
if (instance == null) {
instance = new MySingleton();
}
return instance;
}
protected Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}
}
Approach II
Another approach is to create and assign the object to the static reference variable in the definition itself. Since, static members are initialized only once during class loading, it obviates the need to serialize calls to the static method which returns the singleton.
Example 2:
public final class MySingleton {
private static MySingleton instance = new MySingleton();
private MySingleton() {
}
public static MySingleton getInstance() {
return instance;
}
protected Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}
}
References
http://www.javaworld.com/article/2073352/core-java/simply-singleton.html
A class that can only be instantiated only once is referred to as a Singleton.
In order to design a Singleton class, the following rules should be adopted:
- Private constructors
- This prevents creating objects of the class at will and ensures that object creation can only be done by code inside the class
- It also prevents sub-classing
- Class should be final
- This prevents sub-classing
- It also makes it explicit to the developer that the class cannot be sub-classed
- Private static Object reference
- This is to store the reference to the singleton
- Should be static
- Should be private
- Should be volatile
- Static method that returns the singleton reference
- Should be static
- Should be public
- Should be synchronized. This is to ensure that the method is thread-safe and a single object is created even in multi-threaded scenarios
- Create object if not created
- Return object reference
- Prevent cloning
- Implement Cloneable interface and throw the CloneNotSupportedException
- This prevents cloning of the singleton
Example 1:
public final class MySingleton implements Cloneable {
private static volatile MySingleton instance = null;
private MySingleton() {
}
public static synchronized MySingleton getInstance() {
if (instance == null) {
instance = new MySingleton();
}
return instance;
}
protected Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}
}
Approach II
Another approach is to create and assign the object to the static reference variable in the definition itself. Since, static members are initialized only once during class loading, it obviates the need to serialize calls to the static method which returns the singleton.
Example 2:
public final class MySingleton {
private static MySingleton instance = new MySingleton();
private MySingleton() {
}
public static MySingleton getInstance() {
return instance;
}
protected Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}
}
http://www.javaworld.com/article/2073352/core-java/simply-singleton.html
No comments:
Post a Comment