-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleton.java
More file actions
39 lines (30 loc) · 980 Bytes
/
Singleton.java
File metadata and controls
39 lines (30 loc) · 980 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.atuldwivedi.cp.design.patterns.creational.singleton.serialization;
import java.io.ObjectStreamException;
import java.io.Serializable;
/**
* @author Atul Dwivedi
* @date 03/07/21
* <p>
* An improved thread safe, serializable, lazy-instantiation implementation of Singleton Design Pattern using double locking mechanism.
*/
public class Singleton implements Serializable {
private static Singleton instance;
private Singleton() {
}
public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
//on de-serialization this method will be called and will return singleton instance
protected Object readResolve() throws ObjectStreamException {
return getInstance();
}
public void doSomething() {
}
}