概述
原型模式是一种创建型设计模式,它通过复制一个已经存在的实例来返回新的实例
- 定义:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象
- 适用场景:
- 通过new产生一个对象需要非常繁琐的数据准备或访问权限
- 类初始化需要消化非常多的资源
- 注意:原型模式复制对象直接操作内存,不会触发对象的构造方法,因此与单例模式是冲突的
实现
原型模式可分为浅拷贝和深拷贝
- 浅拷贝:对值类型的成员变量进行值的复制,对引用类型的成员变量只复制引用,不复制引用的对象
- 深拷贝:对值类型的成员变量进行值的复制,对引用类型的成员变量也进行引用对象的复制
浅拷贝
java中实现java.lang.Cloneable即可实现浅拷贝
public class Prototype implements Cloneable {
private int i;
private String str;
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class Client {
public static void main(String[] args) throws Exception {
Prototype p = new Prototype();
String str = "prototype demo";
p.setStr(str);
Prototype p1 = (Prototype) p.clone();
System.out.println(p.getStr() == p1.getStr());
System.out.println(str == p1.getStr());
}
}
输出结果
true
true
输出结果显示p.str、p1.str以及main方法中的str指向的是同一个对象,即Object.clone只是进行的浅拷贝
深拷贝
通过Cloneable接口
理想:按照浅拷贝的套路在clone中将成员变量也拷贝一份再赋值就行了
当实际操作时发现现实并不像理想那样丰满:
- 引用型成员变量可能没有实现Cloneable接口
- 引用型成员变量中可能又有引用型成员变量
按照上面的情况要通过Cloneable接口方式实现深拷贝,就需要所有的引用型成员变量都实现Cloneable接口,而且在引用链上所有的类都必须实现深拷贝,当引用关系复杂时,不要说实现连想想都觉得可怕!
通过序列化接口
劳动人民的智慧是无穷的,Cloneable接口行不通,我们可以使用Serializable接口
public class Prototype implements Serializable {
private static final long serialVersionUID = -7504094503081421405L;
private int i;
private String str;
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
}
public class Client {
public static void main(String[] args) throws Exception {
String str = "prototype demo";
Prototype p = new Prototype();
p.setStr(str);
Prototype p1 = (Prototype) deepClone(p);
System.out.println(p1.getStr());
System.out.println(p.getStr() == p1.getStr());
}
public static Object deepClone(Object obj) throws Exception {
// write
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(obj);
oos.close();
// read
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
Object ret = ois.readObject();
ois.close();
return ret;
}
}
输出结果
prototype demo
false