Important Points to remember:
1) Single to pattern is programming practice to create single Object through out the application.
2)The singleton pattern is a design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. The concept is sometimes generalized to systems that operate more efficiently when only one object exists, or that restrict the instantiation to a certain number of objects.
3)The singleton pattern must be carefully constructed in multi-threaded applications. If two threads are to execute the creation method at the same time when a singleton does not yet exist, they both must check for an instance of the singleton and then only one should create the new one. If the programming language has concurrent processing capabilities the method should be constructed to execute as a mutually exclusive operation.
4)There are two way by which singleton object can be initialized. LAZY and EAGER
Example 1)
1) Single to pattern is programming practice to create single Object through out the application.
2)The singleton pattern is a design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. The concept is sometimes generalized to systems that operate more efficiently when only one object exists, or that restrict the instantiation to a certain number of objects.
3)The singleton pattern must be carefully constructed in multi-threaded applications. If two threads are to execute the creation method at the same time when a singleton does not yet exist, they both must check for an instance of the singleton and then only one should create the new one. If the programming language has concurrent processing capabilities the method should be constructed to execute as a mutually exclusive operation.
4)There are two way by which singleton object can be initialized. LAZY and EAGER
Example 1)
Lazy initialization
public class SingletonDemoLazy{
private static volatile SingletonDemoLazy instance = null;
private SingletonDemoLazy() { }
public static SingletonDemoLazygetInstance() {
if (instance == null) {
synchronized (SingletonDemoLazy.class){
if (instance == null) {
instance = new SingletonDemoLazy();
}
}
}
return instance;
}
public static void main(String[] args) {
Set<SingletonDemoLazy> instances = new HashSet<SingletonDemoLazy>();
instances.add(getInstance());
instances.add(getInstance());
instances.add(getInstance());
instances.add(getInstance());
System.out.println(instances.size());
}
}
Example 2)
Eager initialization
Advantage:
This method has a number of advantages:
- The instance is not constructed until the class is used.
- There is no need to
synchronize
the getInstance()
method, meaning all threads will see the same instance and no (expensive) locking is required.
- The
final
keyword means that the instance cannot be redefined, ensuring that one (and only one) instance ever exists.
public class SingletonDemoEager {
private static final SingletonDemoEager instance = new SingletonDemoEager();
private SingletonDemoEager() {
}
public static SingletonDemoEager getInstance() {
return instance;
}
public static void main(String[] args) {
Set<SingletonDemoEager> instances = new HashSet<SingletonDemoEager>();
instances.add(getInstance());
instances.add(getInstance());
instances.add(getInstance());
instances.add(getInstance());
System.out.println(instances.size());
}
}
Example 3)
Static block initialization
public class Singleton {
private static final Singleton instance;
static {
try {
instance = new Singleton();
} catch (IOException e) {
throw new RuntimeException("Darn, an error occurred!", e);
}
}
public static Singleton getInstance() {
return instance;
}
private Singleton() {
// ...
}
}
Example 4)
Thread-safe without requiring special language constructs (i.e. volatile
or synchronized
).
public class Singleton {
// Private constructor prevents instantiation from other classes
private Singleton() { }
/**
* SingletonHolder is loaded on the first execution of Singleton.getInstance()
* or the first access to SingletonHolder.INSTANCE, not before.
*/
private static class SingletonHolder {
public static final Singleton INSTANCE = new Singleton();
}
public static Singleton getInstance() {
return SingletonHolder.INSTANCE;
}
}
Example 5)
A single-element enum type is the best way to implement a singleton.
public enum Singleton {
INSTANCE;
public void execute (String arg) {
//... perform operation here ...
}
}