Skip to content

Commit f28af6e

Browse files
committed
Example of Singleton Pattern
1 parent 60a629a commit f28af6e

11 files changed

+163
-0
lines changed

.idea/DesignPatterns.iml

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

SerializedSingleton.ser

69 Bytes
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package creational.singleton_pattern;
2+
3+
public class BillPughSingleton {
4+
5+
private BillPughSingleton() {}
6+
7+
private static class SingletonHelper {
8+
private static final BillPughSingleton INSTANCE = new BillPughSingleton();
9+
}
10+
11+
public static BillPughSingleton GetInstance() {
12+
return SingletonHelper.INSTANCE;
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package creational.singleton_pattern;
2+
3+
public class EagerInitializedSingleton {
4+
5+
private static final EagerInitializedSingleton instance = new EagerInitializedSingleton();
6+
7+
private EagerInitializedSingleton() {}
8+
9+
10+
11+
public static EagerInitializedSingleton GetInstance() {
12+
return instance;
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package creational.singleton_pattern;
2+
3+
public enum EnumSingleton {
4+
5+
INSTANCE;
6+
7+
public static void DoSomething() {}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package creational.singleton_pattern;
2+
3+
public class LazyInitializedSingleton {
4+
5+
private static LazyInitializedSingleton instance;
6+
7+
private LazyInitializedSingleton() {}
8+
9+
10+
11+
public static LazyInitializedSingleton GetInstance() {
12+
if (instance == null) {
13+
instance = new LazyInitializedSingleton();
14+
}
15+
return instance;
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package creational.singleton_pattern;
2+
3+
import java.lang.reflect.Constructor;
4+
5+
public class ReflectionSingletonTest {
6+
7+
public static void main(String[] args) {
8+
EagerInitializedSingleton instanceA = EagerInitializedSingleton.GetInstance();
9+
EagerInitializedSingleton instanceB = null;
10+
11+
try {
12+
Constructor[] constructors = EagerInitializedSingleton.class.getDeclaredConstructors();
13+
for (Constructor constructor : constructors) {
14+
// This destroy the singleton pattern
15+
constructor.setAccessible(true);
16+
instanceB = (EagerInitializedSingleton)constructor.newInstance();
17+
break;
18+
}
19+
} catch (Exception e) {
20+
System.out.println(e.getMessage());
21+
}
22+
System.out.println(instanceA.hashCode());
23+
System.out.println(instanceB.hashCode());
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package creational.singleton_pattern;
2+
3+
import java.io.Serializable;
4+
5+
public class SerializedSingleton implements Serializable {
6+
7+
private SerializedSingleton() {}
8+
9+
private static class SingletonHelper {
10+
private static final SerializedSingleton instance = new SerializedSingleton();
11+
}
12+
13+
public static SerializedSingleton GetInstance() {
14+
return SingletonHelper.instance;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package creational.singleton_pattern;
2+
3+
import java.io.*;
4+
5+
public class SerializedSingletonTest {
6+
7+
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
8+
SerializedSingleton instanceA = SerializedSingleton.GetInstance();
9+
ObjectOutput out = new ObjectOutputStream(new FileOutputStream("SerializedSingleton.ser"));
10+
out.writeObject(instanceA);
11+
out.close();
12+
13+
// Deserialize from file to object
14+
ObjectInput input = new ObjectInputStream(new FileInputStream("SerializedSingleton.ser"));
15+
SerializedSingleton instanceB = (SerializedSingleton)input.readObject();
16+
input.close();
17+
18+
System.out.println(instanceA.hashCode());
19+
System.out.println(instanceB.hashCode());
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package creational.singleton_pattern;
2+
3+
public class StaticBlockSingleton {
4+
5+
private static StaticBlockSingleton instance;
6+
7+
private StaticBlockSingleton() {}
8+
9+
10+
11+
// static block initialization for exception handling
12+
static {
13+
try {
14+
instance = new StaticBlockSingleton();
15+
} catch (Exception e) {
16+
throw new RuntimeException("Exception occurred in creating singleton instance");
17+
}
18+
}
19+
20+
public static StaticBlockSingleton GetInstance() {
21+
return instance;
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package creational.singleton_pattern;
2+
3+
public class ThreadSafeSingleton {
4+
5+
private static ThreadSafeSingleton instance;
6+
7+
private ThreadSafeSingleton() {}
8+
9+
10+
public static synchronized ThreadSafeSingleton GetInstance() {
11+
if (instance == null) { instance = new ThreadSafeSingleton(); }
12+
return instance;
13+
}
14+
}

0 commit comments

Comments
 (0)