原型模式的定义
- 原型模式的简单成都仅次于单例模式和迭代器模式。
原型模式的定义:
用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象
原型模式的通用类图如下:
- 原型模式的核心是 clone 方法,通过该方法进行对象的拷贝,Java 提供了一个 Cloneable 接口来标记这个对象是可以拷贝的。因为 Cloneable 接口是一个方法也没有的,只是起到了标记的作用。在 JVM 中只有具有这个标记的对象才有可能被拷贝。那怎么才能从“有可能被拷贝”转换为“可以被拷贝”呢?方法是复写 clone 方法
@Override
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
它是重写的 Object 类中的 clone 方法。
- 原型模式的通用代码
public class PrototypeClass implements Cloneable {
@Override
protected PrototypeClass clone() throws CloneNotSupportedException {
return (PrototypeClass) super.clone();
}
}
实现一个接口,重写 clone 方法就完成了原型模式。
原型模式的优点
- 性能优良
原型模式是在内存二进制流的拷贝,要比直接new一个对象性能要好很多,特别是要在一个循环体内产生大量的对象时,原型模式可以更好的体现其优点。
- 逃避构造函数的约束
这既是它的优点也是它的缺点,直接在内存中拷贝,构造函数是不会执行的。优点是减少了约束,缺点也是减少了约束。
原型模式的使用场景
- 资源优化场景
类初始化需要消化非常多的资源,这个资源包括数据、硬件资源等。
- 性能和安全要求的场景
通过new产生一个对象需要非常繁琐的数据准备或访问权限,则可以使用原型模式。
- 一个对象多个修改者的场景
一个对象需要提供给其他对象访问,而且各个调用者可能都需要修改其值时,可以考虑 使用原型模式拷贝多个对象供调用者使用。
在实际项目中,原型模式很少单独出现,一般是和工厂方法模式一起出现,通过clone的 方法创建一个对象,然后由工厂方法提供给调用者。原型模式已经与Java融为一体,大家可以随手拿来使用。
原型模式的注意事项 (内涵深拷贝和浅拷贝)
- 构造函数不会被执行。
public class PrototypeClass implements Cloneable {
public PrototypeClass() {
System.out.println("我是构造函数...");
}
@Override
protected PrototypeClass clone() {
PrototypeClass prototypeClass = null;
try {
prototypeClass = (PrototypeClass) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return prototypeClass;
}
}
public static void main(String[] args) {
// new 一个对象
PrototypeClass prototypeClass = new PrototypeClass();
// clone 一个对象
PrototypeClass clone = prototypeClass.clone();
}
打印结果只有一条数据
我是构造函数...
对象拷贝时构造函数确实没有被执行,这点从原理来讲也是可以讲得通的,Object类的 clone方法的原理是从内存中(具体地说就是堆内存)以二进制流的方式进行拷贝,重新分配 一个内存块,那构造函数没有被执行也是非常正常的了。
- 浅拷贝和深拷贝
下面是一个例子,在上面的例子中添加一个 ArrayList
public class PrototypeClass implements Cloneable {
private ArrayList<String> arrayList = new ArrayList<>();
@Override
protected PrototypeClass clone() {
PrototypeClass prototypeClass = null;
try {
prototypeClass = (PrototypeClass) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return prototypeClass;
}
public void addValue(String value){
arrayList.add(value);
}
public ArrayList getValue(){
return this.arrayList;
}
}
测试类如下:
public static void main(String[] args) {
// new 一个对象
PrototypeClass prototypeClass = new PrototypeClass();
prototypeClass.addValue("张三");
// clone 一个对象
PrototypeClass clone = prototypeClass.clone();
clone.addValue("李四");
System.out.println(prototypeClass.getValue());
}
打印如下:在 clone 中添加 “李四” 在 prototypeClass 中也添加了“李四”
[张三, 李四]
因为java做了一个懒拷贝的动作, Object对象类提供的方法 clone 只拷贝对象本身,其对象内部的数组引用对象等都不拷贝,还是指向原生对象内部元素的地址,这种拷贝叫做浅拷贝。两个对象共用了一个私有变量,你改大家一块改,是一种非常不安全的方式。
注意:使用原型模式时,引用的成员变量必须满足两个条件才不会被拷贝:一是类的成 员变量,而不是方法内变量;二是必须是一个可变的引用对象,而不是一个原始类型或不可 变对象。
如何通过 clone 进行深拷贝呢?如下:
public class PrototypeClass implements Cloneable {
private ArrayList<String> arrayList = new ArrayList<>();
@Override
protected PrototypeClass clone() {
PrototypeClass prototypeClass = null;
try {
prototypeClass = (PrototypeClass) super.clone();
// 增加了此句
this.arrayList = (ArrayList<String>) this.arrayList.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return prototypeClass;
}
public void addValue(String value){
arrayList.add(value);
}
public ArrayList getValue(){
return this.arrayList;
}
}
public static void main(String[] args) {
// new 一个对象
PrototypeClass prototypeClass = new PrototypeClass();
prototypeClass.addValue("张三");
// clone 一个对象
PrototypeClass clone = prototypeClass.clone();
clone.addValue("李四");
System.out.println(prototypeClass.getValue());
}
代码中仅仅增加了this.arrayList = (ArrayList) this.arrayList.clone();,对所引用的变量进行clone
调用结果如下:
[张三]
这样就实现了深拷贝。两个对象之间就互不影响了。深拷贝还有一种方法就是通过自己写二进制流的方式来操作对象。然后实现对对象的深拷贝。
public static <T> List<T> deepCopy(List<T> src) throws IOException, ClassNotFoundException {
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(byteOut);
out.writeObject(src);
ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
ObjectInputStream in = new ObjectInputStream(byteIn);
@SuppressWarnings("unchecked")
List<T> dest = (List<T>) in.readObject();
return dest;
}
- clone 和 final 关键字是冲突的。如果想clone,在类的成员变量上就不要增加final关键字