|
1 | 1 | package com.anxpp.designpattern.prototype; |
| 2 | + |
2 | 3 | import java.io.File; |
3 | 4 | import java.io.FileInputStream; |
4 | 5 | import java.io.FileOutputStream; |
5 | 6 | import java.io.ObjectInputStream; |
6 | 7 | import java.io.ObjectOutputStream; |
7 | 8 | import java.io.Serializable; |
| 9 | + |
8 | 10 | //使用Serializable支持克隆 |
9 | | -public class SerializablePrototype implements Serializable{ |
10 | | - private static final long serialVersionUID = 1L; |
11 | | - private int i; |
12 | | - private transient int notClone;//transient关键字的成员不会被序列化 |
13 | | - public int getI() { |
14 | | - return i; |
15 | | - } |
16 | | - public void setI(int i) { |
17 | | - this.i = i; |
18 | | - } |
19 | | - public int getNotClone() { |
20 | | - return notClone; |
21 | | - } |
22 | | - public void setNotClone(int notClone) { |
23 | | - this.notClone = notClone; |
24 | | - } |
25 | | - public static long getSerialversionuid() { |
26 | | - return serialVersionUID; |
27 | | - } |
28 | | - public void writeToFile(String path) throws Exception{ |
29 | | - FileOutputStream outStream = new FileOutputStream(path); |
30 | | - ObjectOutputStream objectOutputStream = new ObjectOutputStream(outStream); |
31 | | - objectOutputStream.writeObject(this); |
32 | | - outStream.close(); |
33 | | - } |
34 | | - public SerializablePrototype ReadFromFile(String path) throws Exception{ |
35 | | - File file = new File(path); |
36 | | - if(!file.exists()) |
37 | | - file.createNewFile(); |
38 | | - FileInputStream inStream = new FileInputStream(path); |
39 | | - ObjectInputStream objectOutputStream = new ObjectInputStream(inStream); |
40 | | - Object o= objectOutputStream.readObject(); |
41 | | - inStream.close(); |
42 | | - return (SerializablePrototype) o; |
43 | | - } |
44 | | - public static void main(String args[]) throws Exception{ |
45 | | - String path = "D:/SerializablePrototype.instance"; |
46 | | - SerializablePrototype prototype = new SerializablePrototype(); |
47 | | - prototype.setI(123); |
48 | | - prototype.setNotClone(456); |
49 | | - prototype.writeToFile(path); |
50 | | - SerializablePrototype prototypeClone = new SerializablePrototype(); |
51 | | - prototypeClone = prototype.ReadFromFile(path); |
52 | | - System.out.println(prototypeClone.getI() + " " + prototypeClone.getNotClone() + " "); |
53 | | - } |
| 11 | +public class SerializablePrototype implements Serializable { |
| 12 | + private static final long serialVersionUID = 1L; |
| 13 | + private int i; |
| 14 | + private transient int notClone;//transient关键字的成员不会被序列化 |
| 15 | + |
| 16 | + public int getI() { |
| 17 | + return i; |
| 18 | + } |
| 19 | + |
| 20 | + public void setI(int i) { |
| 21 | + this.i = i; |
| 22 | + } |
| 23 | + |
| 24 | + public int getNotClone() { |
| 25 | + return notClone; |
| 26 | + } |
| 27 | + |
| 28 | + public void setNotClone(int notClone) { |
| 29 | + this.notClone = notClone; |
| 30 | + } |
| 31 | + |
| 32 | + public static long getSerialversionuid() { |
| 33 | + return serialVersionUID; |
| 34 | + } |
| 35 | + |
| 36 | + public void writeToFile(String path) throws Exception { |
| 37 | + FileOutputStream outStream = new FileOutputStream(path); |
| 38 | + ObjectOutputStream objectOutputStream = new ObjectOutputStream(outStream); |
| 39 | + objectOutputStream.writeObject(this); |
| 40 | + outStream.close(); |
| 41 | + } |
| 42 | + |
| 43 | + public SerializablePrototype ReadFromFile(String path) throws Exception { |
| 44 | + File file = new File(path); |
| 45 | + if (!file.exists()) |
| 46 | + file.createNewFile(); |
| 47 | + FileInputStream inStream = new FileInputStream(path); |
| 48 | + ObjectInputStream objectOutputStream = new ObjectInputStream(inStream); |
| 49 | + Object o = objectOutputStream.readObject(); |
| 50 | + inStream.close(); |
| 51 | + return (SerializablePrototype) o; |
| 52 | + } |
| 53 | + |
| 54 | + public static void main(String args[]) throws Exception { |
| 55 | + String path = "SerializablePrototype.instance"; |
| 56 | + SerializablePrototype prototype = new SerializablePrototype(); |
| 57 | + prototype.setI(123); |
| 58 | + prototype.setNotClone(456); |
| 59 | + prototype.writeToFile(path); |
| 60 | + SerializablePrototype prototypeClone = new SerializablePrototype(); |
| 61 | + prototypeClone = prototype.ReadFromFile(path); |
| 62 | + System.out.println(prototypeClone.getI() + " " + prototypeClone.getNotClone() + " "); |
| 63 | + } |
54 | 64 | } |
0 commit comments