Skip to content

Commit c1850aa

Browse files
committed
原型模式
1 parent a7e66db commit c1850aa

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.anxpp.designpattern.prototype;
2+
//使用 java 自带的支持
3+
public class APITestUse {
4+
public static void main(String args[]) throws CloneNotSupportedException{
5+
MyObject myObject = new MyObject();
6+
myObject.i = 500;
7+
MyObject myObjectClone = (MyObject) myObject.clone();
8+
System.out.println(myObjectClone.i);
9+
}
10+
}
11+
class MyObject implements Cloneable{
12+
int i;
13+
public Object clone() throws CloneNotSupportedException{
14+
return super.clone();
15+
}
16+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.anxpp.designpattern.prototype;
2+
import java.io.File;
3+
import java.io.FileInputStream;
4+
import java.io.FileOutputStream;
5+
import java.io.ObjectInputStream;
6+
import java.io.ObjectOutputStream;
7+
import java.io.Serializable;
8+
//使用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+
}
54+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.anxpp.designpattern.prototype;
2+
//具体原型
3+
public class SimplePrototype implements Prototype,Cloneable {
4+
int value;
5+
//clone()实现
6+
@Override
7+
public Object cloneSelf() {
8+
SimplePrototype self = new SimplePrototype();
9+
self.value = value;
10+
return self;
11+
}
12+
//使用
13+
public static void main(String args[]){
14+
SimplePrototype simplePrototype = new SimplePrototype();
15+
simplePrototype.value = 500;
16+
SimplePrototype simplePrototypeClone = (SimplePrototype) simplePrototype.cloneSelf();
17+
System.out.println(simplePrototypeClone.value);
18+
}
19+
}
20+
//抽象原型
21+
interface Prototype{
22+
Object cloneSelf();//克隆自身的方法
23+
}
24+
//客户端使用
25+
class Client{
26+
SimplePrototype prototype;
27+
public Client(SimplePrototype prototype){
28+
this.prototype = prototype;
29+
}
30+
public Object getPrototype(){
31+
return prototype.cloneSelf();
32+
}
33+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* @author Administrator
3+
* 原型模式
4+
*/
5+
package com.anxpp.designpattern.prototype;

0 commit comments

Comments
 (0)