Friday, January 29, 2016

Singleton Pattern

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.
2. How do you 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.
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
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.
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.
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?

7. What are the downsides to using a Singleton Class with `private` Constructor?

Single Responsibility Principle Violation, Because it handles it's own creation in addition to whatever responsibility it handles, it violates SRP. 

No comments:

Post a Comment