1
/** *//**
2
* 部分類的copy方法實(shí)現(xiàn)
3
*
4
*/
5
public class CopyFactoryImpl implements CopyFactory
{
6
7
public Object copy(Object from)
{
8
if(from != null)
{
9
// if(from instanceof Params)
10
// return copyParams((Params)from);
11
// if(from instanceof Value)
12
// return copyValue((Value)from);
13
// if(from instanceof OvertimePolicies)
14
// return copyPolicies((OvertimePolicies)from);
15
// if(from instanceof Event)
16
// return copyEvent((Event)from);
17
18
return copyObject(from);//from應(yīng)Serialization
19
20
}
21
22
return null;
23
}
24
25
26
/** *//**
27
* 緩存復(fù)制方式拷貝
28
* @param from
29
* @return
30
*/
31
public Object copyObject(Object from)
{
32
try
{
33
// 在內(nèi)存中開(kāi)辟一塊緩沖區(qū),用于將源對(duì)象寫(xiě)入
34
ByteArrayOutputStream bout = new ByteArrayOutputStream();
35
ObjectOutputStream out = new ObjectOutputStream(bout);
36
//通過(guò)Serialization機(jī)制將自身寫(xiě)入該緩沖區(qū)
37
out.writeObject(from);
38
out.close();
39
40
// 找到剛才開(kāi)辟的緩沖區(qū)準(zhǔn)備讀取
41
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
42
ObjectInputStream in = new ObjectInputStream(bin);
43
//將剛才寫(xiě)入的內(nèi)容讀入目標(biāo)對(duì)象
44
Object target = in.readObject();
45
in.close();
46
47
//返回目標(biāo)對(duì)象,拷貝完畢
48
return target;
49
}catch (Exception e)
{
50
return null;
51
}
52
}
53
}


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
