Skip to content

Commit 39704b5

Browse files
committed
Break Singleton Design Pattern
1 parent 5c7d2e5 commit 39704b5

File tree

11 files changed

+137
-0
lines changed

11 files changed

+137
-0
lines changed
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package BreakSingleton.throughreflection;
2+
3+
public class AlreadyExistException extends RuntimeException {
4+
5+
public AlreadyExistException(String msg) {
6+
7+
super(msg);
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package BreakSingleton.throughreflection;
2+
3+
import java.lang.reflect.Constructor;
4+
5+
public class Main {
6+
7+
/**
8+
* @param args
9+
*/
10+
public static void main(String[] args) {
11+
12+
Singleton s1 = Singleton.getIntance();
13+
Singleton s2 = null;
14+
try {
15+
Constructor<Singleton> constructor = Singleton.class.getDeclaredConstructor();
16+
constructor.setAccessible(true);
17+
s2 = constructor.newInstance();
18+
} catch (Exception e) {
19+
20+
e.printStackTrace();
21+
}
22+
System.out.println(s1.hashCode());
23+
System.out.println(s2.hashCode());
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package BreakSingleton.throughreflection;
2+
3+
public class Singleton {
4+
5+
private static Singleton s;
6+
private Singleton() {
7+
if(s != null) {
8+
9+
throw new AlreadyExistException("Instance Already Exists");
10+
}
11+
}
12+
13+
public static Singleton getIntance() {
14+
if(s == null) {
15+
16+
synchronized(Singleton.class) {
17+
18+
if(s == null) {
19+
20+
s = new Singleton();
21+
}
22+
}
23+
}
24+
return s;
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package BreakSingleton.throughserialization;
2+
3+
import java.io.FileInputStream;
4+
import java.io.FileOutputStream;
5+
import java.io.ObjectInputStream;
6+
import java.io.ObjectOutputStream;
7+
8+
public class Main {
9+
public static void main(String[] args) {
10+
11+
Singleton s1 = Singleton.getInstance();
12+
Singleton s2 = null;
13+
try {
14+
15+
FileOutputStream fos = new FileOutputStream("singleton.do");
16+
ObjectOutputStream oos = new ObjectOutputStream(fos);
17+
18+
oos.writeObject(s1);
19+
20+
fos.close();
21+
oos.close();
22+
} catch(Exception e) {
23+
24+
e.printStackTrace();
25+
}
26+
27+
try {
28+
29+
FileInputStream fis = new FileInputStream("singleton.do");
30+
ObjectInputStream ois = new ObjectInputStream(fis);
31+
32+
s2 = (Singleton)ois.readObject();
33+
34+
fis.close();
35+
ois.close();
36+
} catch(Exception e) {
37+
38+
e.printStackTrace();
39+
}
40+
41+
System.out.println(s1.hashCode());
42+
System.out.println(s2.hashCode());
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package BreakSingleton.throughserialization;
2+
3+
import java.io.Serializable;
4+
import BreakSingleton.throughreflection.AlreadyExistException;
5+
6+
public class Singleton implements Serializable {
7+
private static Singleton s;
8+
private Singleton() {
9+
10+
if(s != null) {
11+
12+
throw new AlreadyExistException("Instance already exist");
13+
}
14+
}
15+
public static Singleton getInstance() {
16+
17+
if(s == null) {
18+
19+
synchronized(Singleton.class) {
20+
21+
if(s == null) {
22+
23+
s = new Singleton();
24+
}
25+
}
26+
}
27+
return s;
28+
}
29+
public Object readResolve() {
30+
31+
return s;
32+
}
33+
}

0 commit comments

Comments
 (0)