原型模式的思想就是将一个对象作为原型,对其进行复制、克隆,产生一个和原对象类似的新对象。
说道复制对象,我将结合对象的浅复制和深复制来说一下,首先需要了解对象深、浅复制的概念:
浅复制:将一个对象复制后,基本数据类型的变量都会重新创建,而引用类型,指向的还是原对象所指向的。
深复制:将一个对象复制后,不论是基本数据类型还有引用类型,都是重新创建的。简单来说,就是深复制进行了完全彻底的复制,而浅复制不彻底。
对象深浅复制的例子:
package com.panther.demo.exercise9;
import java.io.*;
/**
* 对象的深复制和浅复制
* Created by panther.dongdong on 2016/3/15.
*/
public class Prototype implements Cloneable, Serializable {
private static final long serialVersionUID = -3346888925332166469L;
private String string;
private SerializableObject serializableObject;
//浅复制
public Prototype clone() throws CloneNotSupportedException {
Prototype prototype = (Prototype) super.clone();
return prototype;
}
//深复制
public Prototype deepClone() throws IOException, ClassNotFoundException {
//写入当前对象的二进制流
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteOutputStream);
objectOutputStream.writeObject(this);
//读出二进制流产生新对象
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteOutputStream.toByteArray());
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
return (Prototype) objectInputStream.readObject();
}
public String getString() {
return string;
}
public void setString(String string) {
this.string = string;
}
public SerializableObject getSerializableObject() {
return serializableObject;
}
public void setSerializableObject(SerializableObject serializableObject) {
this.serializableObject = serializableObject;
}
static class SerializableObject implements Serializable {
private static final long serialVersionUID = -7728443971130605297L;
}
public static void main(String[] args) throws CloneNotSupportedException, IOException, ClassNotFoundException {
Prototype prototype = new Prototype();
prototype.setSerializableObject(new SerializableObject()); //设置引用类型对象
Prototype prototype1 = prototype.clone(); //浅复制
Prototype prototype2 = prototype.deepClone(); //深复制
System.out.println(prototype.getSerializableObject() == prototype1.getSerializableObject()); //true
System.out.println(prototype.getSerializableObject() == prototype2.getSerializableObject()); //false
}
}