原型模式基本介绍
原型模式(Prototype模式)是指用原型实例指定创建对象的种类,并且的通过拷贝这些原型,创建新对象。原型模式是一种创建型模式,允许一个对象再创建另外一个可定制的对象,无需知道如何创建的细节。
原型模式的共偶作原理是:通过将一个原型对象传给哪个需要发动创建的对象,这个要发动创建的对象通过请求原型对象拷贝他们自己来实施创建。即 对象.clone()
原型模式的UML图
说明:Prototype:原型类,声明自己克隆一个自己的接口
ConcretePrototype:具体的原型类,实现一个克隆自己的操作
Client:让一个原型对象克隆自己。从而创建一个新的对象(属性一样)
案例:克隆羊问题
现有一只羊Tom。姓名为Tom,年龄为1,颜色为白色,请编写程序创建和Tom羊属性完全相同的10只羊。
代码
sheep:
public class Sheep implements Cloneable {
public Sheep(String name, int age, String color) {
this.name = name;
this.age = age;
this.color = color;
}
private String name;
private int age;
private String color;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return "Sheep{" +
"name='" + name + '\'' +
", age=" + age +
", color='" + color + '\'' +
'}';
}
@Override
protected Object clone() {
Sheep sheep = null;
try {
sheep = (Sheep) super.clone();
} catch (Exception e) {
e.printStackTrace();
}
return sheep;
}
}
Client调用:
public class Client {
public static void main(String[] args) {
Sheep sheep = new Sheep("多利", 1, "白色");
Sheep sheep1 = (Sheep) sheep.clone();
Sheep sheep2 = (Sheep) sheep.clone();
Sheep sheep3 = (Sheep) sheep.clone();
System.out.println(sheep);
System.out.println(sheep1);
System.out.println(sheep2);
System.out.println(sheep3);
}
}
深拷贝和浅拷贝
- 浅拷贝
对于数据类型是基本数据类型的成员变量,浅拷贝会直接进行值传递,也就是将该属性值复制一份直接给新的对象。对于数据类型是引用类型的成员变量,比如说成员变量是某个数组,某个类的对象等,那么浅拷贝会进行引用传递,也就是只是将该成员变量的引用值(内存地址)复制一份给新的对象。因为实际上两个对象的该成员变量都指向同一个实例,在这种情况下,在一个对象中修改该成员变量会影响到另一个对象的该成员变量的值。浅拷贝都时使用默认的 clone()方法来实现。
- 深拷贝
复制对象的所有基本数据类型的成员变量值。为所有引用数据类型的成员变量申请存储给空间,并复制每个引用数据类型成员变量所引用你的对象,直到该对象可达所有对象。也就是说,对象进行深拷贝要对整个对象(包括对象的引用数据类型)进行拷贝。
深拷贝的实现方式:重写clone方法来实现深拷贝;通过对象序列化实现深拷贝(推荐)
深拷贝案例
原型类:
import java.io.Serializable;
public class DeepCloneableTarget implements Cloneable, Serializable {
private String cloneName;
private String cloneClass;
public DeepCloneableTarget(String cloneName, String cloneClass) {
this.cloneName = cloneName;
this.cloneClass = cloneClass;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
实现深拷贝的类:
import java.io.*;
public class DeepProtoType implements Cloneable, Serializable {
public String name;
public DeepCloneableTarget deepCloneableTarget;
//深拷贝实现方式一:使用clone方法
@Override
protected Object clone() throws CloneNotSupportedException {
Object deep = super.clone();
DeepProtoType deepProtoType = (DeepProtoType) deep;
//对引用类型的数据单独处理
DeepCloneableTarget deepCloneableTarget = deepProtoType.deepCloneableTarget;
deepProtoType.deepCloneableTarget = (DeepCloneableTarget) deepCloneableTarget.clone();
return deep;
}
//深拷贝实现方式二:通过对象的序列化方法实现
public Object deepClone() {
ByteArrayOutputStream bos = null;
ByteArrayInputStream bis = null;
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
try {
//序列化
bos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(bos);
oos.writeObject(this);
//反序列化
bis = new ByteArrayInputStream(bos.toByteArray());
ois = new ObjectInputStream(bis);
DeepProtoType deep = (DeepProtoType) ois.readObject();
return deep;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try {
bos.close();
bis.close();
oos.close();
ois.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}