@@ -23,22 +23,6 @@ public class CloneTest {
23
23
// *******************************************
24
24
// 浅克隆
25
25
// *******************************************
26
-
27
- public static <T > T deepClone (T object ) {
28
- try {
29
- ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream ();
30
- ObjectOutputStream objectOutputStream = new ObjectOutputStream (byteArrayOutputStream );
31
- objectOutputStream .writeObject (object );
32
- ByteArrayInputStream bais =
33
- new ByteArrayInputStream (byteArrayOutputStream .toByteArray ());
34
- ObjectInputStream objectInputStream = new ObjectInputStream (bais );
35
- return (T ) objectInputStream .readObject ();
36
- } catch (Exception e ) {
37
- e .printStackTrace ();
38
- return null ;
39
- }
40
- }
41
-
42
26
@ Test
43
27
public void testShadowClone4Map () {
44
28
HashMap <String , Address > map = new HashMap <>();
@@ -49,10 +33,6 @@ public void testShadowClone4Map() {
49
33
Assert .isTrue (clone .get ("nanjing" ) == map .get ("nanjing" ));
50
34
}
51
35
52
- // *******************************************
53
- // 深克隆
54
- // *******************************************
55
-
56
36
@ Test
57
37
public void testShadowClone4List () {
58
38
ArrayList <Address > list = new ArrayList <>();
@@ -63,6 +43,23 @@ public void testShadowClone4List() {
63
43
Assert .isTrue (clone .get (0 ) == list .get (0 ));
64
44
}
65
45
46
+ // *******************************************
47
+ // 深克隆1: 序列化(小推荐)
48
+ // *******************************************
49
+ public static <T > T deepClone (T object ) {
50
+ try {
51
+ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream ();
52
+ ObjectOutputStream objectOutputStream = new ObjectOutputStream (byteArrayOutputStream );
53
+ objectOutputStream .writeObject (object );
54
+ ByteArrayInputStream bais =
55
+ new ByteArrayInputStream (byteArrayOutputStream .toByteArray ());
56
+ ObjectInputStream objectInputStream = new ObjectInputStream (bais );
57
+ return (T ) objectInputStream .readObject ();
58
+ } catch (Exception e ) {
59
+ e .printStackTrace ();
60
+ return null ;
61
+ }
62
+ }
66
63
/** 需要 Address & Employee 实现序列化接口 */
67
64
@ Test
68
65
public void whenModifyingOriginalObject_thenSerializableShouldNotChange () {
@@ -76,6 +73,9 @@ public void whenModifyingOriginalObject_thenSerializableShouldNotChange() {
76
73
Assert .isFalse (deepCopy .getAddress ().getCity ().equals (pm .getAddress ().getCity ()));
77
74
}
78
75
76
+ // *******************************************
77
+ // 深克隆2: json(obj-str-obj)(小推荐)
78
+ // *******************************************
79
79
/** 不需要 Address & Employee 实现序列化接口 */
80
80
@ Test
81
81
public void whenModifyingOriginalObject_thenGsonCloneShouldNotChange () {
@@ -89,11 +89,7 @@ public void whenModifyingOriginalObject_thenGsonCloneShouldNotChange() {
89
89
Assert .isFalse (deepCopy .getAddress ().getCity ().equals (pm .getAddress ().getCity ()));
90
90
}
91
91
92
- /**
93
- * 不需要 Address & Employee 实现序列化接口
94
- *
95
- * @throws IOException
96
- */
92
+ /** 不需要 Address & Employee 实现序列化接口 */
97
93
@ Test
98
94
public void whenModifyingOriginalObject_thenJacksonCopyShouldNotChange () throws IOException {
99
95
Address address = new Address ("xuzhou" , "jiangsu" );
@@ -104,4 +100,14 @@ public void whenModifyingOriginalObject_thenJacksonCopyShouldNotChange() throws
104
100
address .setCity ("shanghai" );
105
101
Assert .isFalse (deepCopy .getAddress ().getCity ().equals (pm .getAddress ().getCity ()));
106
102
}
103
+
104
+ // *******************************************
105
+ // 深克隆3: 递归 clone(小推荐), 逻辑上判断后复制
106
+ // *******************************************
107
+
108
+ // *******************************************
109
+ // 深克隆4: 重写最外层对象的 clone(不推荐)
110
+ // @see Employee#clone()
111
+ // *******************************************
112
+
107
113
}
0 commit comments