-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution.java
More file actions
128 lines (99 loc) · 5.16 KB
/
Solution.java
File metadata and controls
128 lines (99 loc) · 5.16 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//Then they all shouted again, saying, "Not this man, but Barabbas!" Now Barabbas was a robber (John 18:40)
package com.javarush.task.task20.task2008;
import java.io.*;
/*
Как сериализовать Singleton?
*/
public class Solution implements Serializable {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Singleton instance = Singleton.getInstance();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//Serializing the singleton instance
ObjectOutputStream oos = new ObjectOutputStream(byteArrayOutputStream);
oos.writeObject(instance);
oos.close();
//Recreating the instance by reading the serialized object data add
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
ObjectInputStream ois = new ObjectInputStream(byteArrayInputStream);
Singleton singleton = (Singleton) ois.readObject();
ois.close();
//Recreating the instance AGAIN by reading the serialized object data add
byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
ObjectInputStream ois2 = new ObjectInputStream(byteArrayInputStream);
Singleton singleton1 = (Singleton) ois2.readObject();
ois2.close();
//The singleton behavior has been broken
System.out.println("Instance reference check : " + singleton.getInstance());
System.out.println("Instance reference check : " + singleton1.getInstance());
System.out.println("=========================================================");
System.out.println("Object reference check : " + singleton);
System.out.println("Object reference check : " + singleton1);
}
public static class Singleton implements Serializable {
private static Singleton ourInstance;
public static Singleton getInstance() {
if (ourInstance == null) {
ourInstance = new Singleton();
}
return ourInstance;
}
private Singleton() {
}
private Object readResolve() throws ObjectStreamException {
return getInstance();
}
}
}
/*
Как сериализовать Singleton?
Два десериализованных объекта singleton и singleton1 имеют разные ссылки в памяти, а должны иметь одинаковые.
В класс Singleton добавь один метод (погуглите), чтобы после десериализации ссылки на объекты были равны.
Метод main не участвует в тестировании.
Требования:
1. Класс Solution.Singleton должен поддерживать интерфейс Serializable.
2. В классе Solution.Singleton должен быть реализован метод readResolve без параметров.
3. Метод readResolve должен возвращать синглтон (ourInstance).
4. Метод readResolve должен быть приватным.
package com.javarush.task.task20.task2008;
import java.io.Serializable;
*
Как сериализовать Singleton?
*
public class Solution implements Serializable {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Singleton instance = Singleton.getInstance();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//Serializing the singleton instance
ObjectOutputStream oos = new ObjectOutputStream(byteArrayOutputStream);
oos.writeObject(instance);
oos.close();
//Recreating the instance by reading the serialized object data add
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
ObjectInputStream ois = new ObjectInputStream(byteArrayInputStream);
Singleton singleton = (Singleton) ois.readObject();
ois.close();
//Recreating the instance AGAIN by reading the serialized object data add
byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
ObjectInputStream ois2 = new ObjectInputStream(byteArrayInputStream);
Singleton singleton1 = (Singleton) ois2.readObject();
ois2.close();
//The singleton behavior has been broken
System.out.println("Instance reference check : " + singleton.getInstance());
System.out.println("Instance reference check : " + singleton1.getInstance());
System.out.println("=========================================================");
System.out.println("Object reference check : " + singleton);
System.out.println("Object reference check : " + singleton1);
}
public static class Singleton implements Serializable {
private static Singleton ourInstance;
public static Singleton getInstance() {
if (ourInstance == null) {
ourInstance = new Singleton();
}
return ourInstance;
}
private Singleton() {
}
}
}
*/