原型模式:将对象作为一个原型,进行复制克隆,产生一个和原对象相似的新对象
浅复制与深复制的区别:
主要区别在于对于要复制对象中的引用:浅复制只是新建一个引用,指向的还要是原来的内存;深复制会新分配内存,新引用指向 新内存
/* 浅复制 */
public Object clone() throws CloneNotSupportedException {
Prototype proto = (Prototype) super.clone();
return proto;
}
/* 深复制 */
public Object deepClone() throws IOException, ClassNotFoundException {
/* 写入当前对象的二进制流 */
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(this);
/* 读出二进制流产生的新对象 */
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
return ois.readObject();
}
输出结果:
浅复制与深复制的区别:
主要区别在于对于要复制对象中的引用:浅复制只是新建一个引用,指向的还要是原来的内存;深复制会新分配内存,新引用指向 新内存
/* 浅复制 */
public Object clone() throws CloneNotSupportedException {
Prototype proto = (Prototype) super.clone();
return proto;
}
/* 深复制 */
public Object deepClone() throws IOException, ClassNotFoundException {
/* 写入当前对象的二进制流 */
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(this);
/* 读出二进制流产生的新对象 */
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
return ois.readObject();
}
要实现深复制,需要采用流的形式读入当前对象的二进制输入,再写出二进制数据对应的对象。
验证代码如下:
import java.io.*;
public class Prototype implements Cloneable,Serializable {
private static final long serialVersionUID = 1L;
private String string;
private SerializableObject obj;
/* 浅复制 */
public Object clone() throws CloneNotSupportedException {
Prototype proto = (Prototype) super.clone();
return proto;
}
/* 深复制 */
public Object deepClone() throws IOException,ClassNotFoundException,CloneNotSupportedException{
/* 写入当前对象的二进制流 */
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(this);
/* 读出二进制流产生的新对象 */
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
return ois.readObject();
}
public String getString() {
return string;
}
public void setString(String string) {
this.string = string;
}
public SerializableObject getObj() {
return obj;
}
public void setObj(SerializableObject obj) {
this.obj = obj;
}
public static void main(String []args)throws CloneNotSupportedException,IOException,ClassNotFoundException {
SerializableObject testSerializableObject=new SerializableObject();
String str="teststring";
Prototype pro=new Prototype();
pro.setString(str);
pro.setObj(testSerializableObject);
Prototype proclone=(Prototype)pro.clone();
Prototype prodeepclone=(Prototype)pro.deepClone();
System.out.println(proclone.getObj()==pro.getObj());
System.out.println(prodeepclone.getObj()==pro.getObj());
}
}
class SerializableObject implements Serializable {
private static final long serialVersionUID = 1L;
}
输出结果:
true
false
可见对于浅复制,复制对象的引用所指向的内存还是原来的,而深复制是新开了一片内存
详见http://blog.youkuaiyun.com/zhangerqing/article/details/8194653