Singleton Pattern
1. What is singleton pattern?
The singleton pattern is a design pattern used to create single instance of class (only one object) thought out the application.
The singleton pattern is a design pattern used to create single instance of class (only one object) thought out the application.
2. How do you implement singleton pattern in java?
There are two ways to implement singleton pattern in java:
There are two ways to implement singleton pattern in java:
1
2
3
4
5
6
7
8
9
10
11
12
13
| public class Singleton { private static Singleton singleInstance; // Make constructor as private to ensure that not able to instantiate a class outside private Singleton() { } public static Singleton getInstance() { if (singleInstance == null ) { singleInstance = new Singleton(); } return singleInstance; } |
another way:
1
2
3
4
5
6
7
8
9
10
| public class Singleton { private static Singleton singleInstance = new Singleton(); // Make constructor as private private Singleton() { } public static Singleton getInstance() { return singleInstance; } |
3. How do you make sure that it works in multi threaded environment?
This is simple and thread-safe singleton implementation.
This is simple and thread-safe singleton implementation.
1
2
3
4
5
6
7
| public static synchronized Singleton getInstance() { if (singleInstance == null ) { singleInstance = new Singleton(); } return singleInstance; } |
Synchronizing block (double-checked locking) instead of entire method for better performance, but unfortunately, it does not work.
1
2
3
4
5
6
7
8
9
10
11
| // Double-checked locking -- don't use public static Singleton getInstance() { if (singleInstance == null ) { synchronized (Singleton. class ) { if (singleInstance == null ) { singleInstance = new Singleton(); } } } return singleInstance; } |
An alternative thread-safe singleton implementation:
This is simple, fast, and thread-safe singleton implementation
This is simple, fast, and thread-safe singleton implementation
1
2
3
4
5
6
7
| public class Singleton { public final static Singleton INSTANCE = new Singleton(); private Singleton() { // Exists only to defeat instantiation. } } |
4. How do you prevent creating another instance of Singleton using clone() method?
We can create another instance of class as shown below.
We can create another instance of class as shown below.
1
2
3
4
5
6
7
8
9
10
11
12
| public class Clone { public static void main(String args[]) throws Exception { // Get a singleton Singleton obj = Singleton.getInstance(); // Let's clone the object Singleton clone = (Singleton) obj.clone(); } } |
We can prevent another instance of class by overriding the Object’s clone method which throws a CloneNotSupportedException exception.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| public class Singleton { private static Singleton singleInstance; // Make constructor as private private Singleton() { } public static Singleton getInstance() { if (singleInstance == null ) { singleInstance = new Singleton(); } return singleInstance; } // Override the Object’s clone method and throw CloneNotSupportedException exception public Object clone() throws CloneNotSupportedException { throw new CloneNotSupportedException(); } |
5. How do you prevent creating another instance of Singleton during serialization?
If the Singleton class implements the java.io.Serializable interface, when a singleton is serialized and then deserialized more than once, there will be multiple instances of Singleton created. In order to avoid this the readResolve method should be overridden and all instance fields should be declared as transient (to prevent a serialization attack) . See Serializable () and readResolve Method () in javadocs.
If the Singleton class implements the java.io.Serializable interface, when a singleton is serialized and then deserialized more than once, there will be multiple instances of Singleton created. In order to avoid this the readResolve method should be overridden and all instance fields should be declared as transient (to prevent a serialization attack) . See Serializable () and readResolve Method () in javadocs.
1
2
3
4
5
6
7
8
9
| public class Singleton implements java.io.Serializable { ... // This method is called immediately after an object of this class is deserialized. // This method returns the singleton instance. private Object readResolve() { return getInstance(); } } |
6. How do you prevent creating another instance of Singleton using reflection?
Single Responsibility Principle Violation, Because it handles it's own creation in addition to whatever responsibility it handles, it violates SRP.
Useful References:
1. https://www.securecoding.cert.org/confluence/display/java/MSC07-J.+Prevent+multiple+instantiations+of+singleton+objects
2. http://www.javaworld.com/javaworld/jw-04-2003/jw-0425-designpatterns.html?page=1
3. http://java.sun.com/developer/technicalArticles/Programming/singletons/
1. https://www.securecoding.cert.org/confluence/display/java/MSC07-J.+Prevent+multiple+instantiations+of+singleton+objects
2. http://www.javaworld.com/javaworld/jw-04-2003/jw-0425-designpatterns.html?page=1
3. http://java.sun.com/developer/technicalArticles/Programming/singletons/