forked from shabbirdwd53/design_patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
49 lines (45 loc) · 2.9 KB
/
Main.java
File metadata and controls
49 lines (45 loc) · 2.9 KB
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
40
41
42
43
44
45
46
47
48
49
// References https://www.digitalocean.com/community/tutorials/java-singleton-design-pattern-best-practices-examples
//https://www.geeksforgeeks.org/java-singleton-design-pattern-practices-examples/
import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
//Before jumping into pattern just explain what is lazy loading and eager loading
// Mainly this class used to show violations using serializable and reflection.
public class Main {
public static void main(String[] args) throws IOException, ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException {
//exampleSerialization();
exampleReflection();
}
private static void exampleSerialization() throws IOException, ClassNotFoundException {
LazySingleton lazySingleton = LazySingleton.getInstance();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("object.obj"));
objectOutputStream.writeObject(lazySingleton);
objectOutputStream.close();
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("object.obj"));
LazySingleton deserializedLazy = (LazySingleton) objectInputStream.readObject();
objectInputStream.close();
System.out.println("Object 1 :" + lazySingleton.hashCode());
System.out.println("Object 2 :" + deserializedLazy.hashCode());
SerializableSingleton serializableSingleton = SerializableSingleton.getInstance();
ObjectOutputStream objectOutputStream2 = new ObjectOutputStream(new FileOutputStream("object1.obj"));
objectOutputStream2.writeObject(serializableSingleton);
objectOutputStream2.close();
ObjectInputStream objectInputStream2 = new ObjectInputStream(new FileInputStream("object1.obj"));
SerializableSingleton deserializedInstance = (SerializableSingleton) objectInputStream2.readObject();
objectInputStream2.close();
System.out.println("SerializableSingleton Object 1 :" + serializableSingleton.hashCode());
System.out.println(" SerializableSingleton Object 2 :" + deserializedInstance.hashCode());
}
private static void exampleReflection() throws InvocationTargetException, InstantiationException, IllegalAccessException {
Constructor[] constructors = LazySingleton.class.getDeclaredConstructors();
//Knowing only one constructor taking it using index
Constructor constructor = constructors[0];
constructor.setAccessible(true);
LazySingleton lazySingleton = (LazySingleton) constructor.newInstance();
LazySingleton instance = LazySingleton.getInstance();
System.out.println("Reflected hashcode singleton :"+lazySingleton.hashCode());
System.out.println("Singleton instance : "+ instance.hashCode());
//Solution to this is go by enum
EnumSingleton.INSTANCE.doSomething();
}
}