传统方法
现在有一只羊tom,姓名为:tom,年龄为:1,颜色为:白色。编写程序创建和tom羊属性完全相同的10只羊。
代码示例
public class Sheep {
private String name;
private int age;
private String color;
public Sheep(String name, int age, String color) {
this.name = name;
this.age = age;
this.color = 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 + '\'' +
'}';
}
}
public class Client {
public static void main(String[] args) {
Sheep sheep1 = new Sheep("tom", 1, "白色");
Sheep sheep2 = new Sheep(sheep1.getName(), sheep1.getAge(), sheep1.getColor());
Sheep sheep3 = new Sheep(sheep1.getName(), sheep1.getAge(), sheep1.getColor());
Sheep sheep4 = new Sheep(sheep1.getName(), sheep1.getAge(), sheep1.getColor());
Sheep sheep5 = new Sheep(sheep1.getName(), sheep1.getAge(), sheep1.getColor());
System.out.println(sheep1);
System.out.println(sheep2);
System.out.println(sheep3);
System.out.println(sheep4);
System.out.println(sheep5);
}
}
优缺点
- 优点是比较好理解,简单易操作。
- 在创建新的对象时,总是需要重新获取原始对象的属性,如果创建的对象比较复杂时,效率较低。
- 总是需要重新初始化对象,而不是动态地获取对象运行时的状态,不够灵活。
原型模式
基本介绍
- 原型模式是指:用原型实例制定创建对象的种类,并且通过拷贝这些原型,创建新的对象。
- 原型模式是一种创建型设计模式,允许一个对象再创建另外一个可定制的对象,无需知道如何创建的细节。
- 工作原理是:通过将一个原型对象传给那个要发动创建的对象,这个要发动创建的对象通过请求原型对象拷贝他们自己来实施创建。
代码示例
public class Sheep implements Cloneable{
private String name;
private int age;
private String color;
public Sheep(String name, int age, String color) {
this.name = name;
this.age = age;
this.color = 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 + '\'' +
'}';
}
//使用默认的clone方法来完成
@Override
protected Object clone(){
Sheep sheep = null;
try {
sheep = (Sheep) super.clone();
}catch (Exception e){
e.printStackTrace();
}
return sheep;
}
}
public class Client {
public static void main(String[] args) {
Sheep sheep1 = new Sheep("tom", 1, "白色");
Sheep sheep2 = (Sheep) sheep1.clone();
Sheep sheep3 = (Sheep) sheep1.clone();
Sheep sheep4 = (Sheep) sheep1.clone();
Sheep sheep5 = (Sheep) sheep1.clone();
System.out.println(sheep1);
System.out.println(sheep2);
System.out.println(sheep3);
System.out.println(sheep4);
System.out.println(sheep5);
}
}
浅拷贝
- 对于数据类型是基本数据类型的成员变量,浅拷贝会直接进行值传递,也就是将该属性复制一份给新的对象。
- 对于数据类型是引用数据类型的成员变量,比如说成员变量是某个数组、某个类的对象等,那么浅拷贝会进行引用传递,也就是只是将该成员变量的引用值(内存地址)复制一份给新的对象。因为实际上两个对象的该成员变量都只想同一个实例。在这种情况下,在一个对象中修改该成员变量会影响到另一个该成员变量的值。
- 克隆羊就是浅拷贝。
- 浅拷贝就是使用默认的clone()方法来实现。
深拷贝
- 复制对象的所有基本数据类型的成员变量值。
- 为所有引用数据类型的成员变量申请存储空间,并复制每个引用数据类型成员变量所引用的对象,直到该对象可达的所有对象。也就是说,对象进行深拷贝要对整个对象进行拷贝。
- 深拷贝实现方式1:重写clone()方法。
- 深拷贝实现方式2:通过对象序列化。
代码示例
public class DeepCloneTarget implements Serializable, Cloneable {
private static final long serialVersionUID = 1L;
private String cloneName;
private String cloneClass;
public DeepCloneTarget(String cloneName, String cloneClass) {
this.cloneName = cloneName;
this.cloneClass = cloneClass;
}
public static long getSerialVersionUID() {
return serialVersionUID;
}
public String getCloneName() {
return cloneName;
}
public void setCloneName(String cloneName) {
this.cloneName = cloneName;
}
public String getCloneClass() {
return cloneClass;
}
public void setCloneClass(String cloneClass) {
this.cloneClass = cloneClass;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class DeepProtoType implements Serializable, Cloneable {
private String name;
private DeepCloneTarget deepCloneTarget;
public DeepProtoType(String name, DeepCloneTarget deepCloneTarget) {
this.name = name;
this.deepCloneTarget = deepCloneTarget;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public DeepCloneTarget getDeepCloneTarget() {
return deepCloneTarget;
}
public void setDeepCloneTarget(DeepCloneTarget deepCloneTarget) {
this.deepCloneTarget = deepCloneTarget;
}
//方式1:使用clone方法
@Override
protected Object clone() throws CloneNotSupportedException {
Object deep = null;
//完成对基本数据类型属性的克隆
deep = super.clone();
//对引用类型进行单独处理
DeepProtoType deepProtoType = (DeepProtoType) deep;
deepProtoType.deepCloneTarget = (DeepCloneTarget) deepCloneTarget.clone();
return deepProtoType;
}
//方式2:通过对象的序列化来实现(推荐)
public Object deepClone(){
//创建流对象
ByteArrayOutputStream byteArrayOutputStream = null;
ByteArrayInputStream byteArrayInputStream = null;
ObjectOutputStream objectOutputStream = null;
ObjectInputStream objectInputStream = null;
try {
//序列化
byteArrayOutputStream = new ByteArrayOutputStream();
objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(this);//将当前对象以对象流的方式输出
//反序列化
byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
objectInputStream = new ObjectInputStream(byteArrayInputStream);
return (DeepProtoType) objectInputStream.readObject();
}catch (Exception e){
e.printStackTrace();
return null;
}finally {
try {
byteArrayOutputStream.close();
byteArrayInputStream.close();
objectOutputStream.close();
objectInputStream.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
public class Client {
public static void main(String[] args) throws CloneNotSupportedException {
//方式1
DeepProtoType deepProtoType1 = new DeepProtoType("123", new DeepCloneTarget("name", "class"));
DeepProtoType deepProtoType2 = (DeepProtoType) deepProtoType1.clone();
System.out.println(deepProtoType1.hashCode());
System.out.println(deepProtoType2.hashCode());
System.out.println(deepProtoType1.getDeepCloneTarget().hashCode());
System.out.println(deepProtoType2.getDeepCloneTarget().hashCode());
//方式2
DeepProtoType deepProtoType3 = new DeepProtoType("123", new DeepCloneTarget("name", "class"));
DeepProtoType deepProtoType4 = (DeepProtoType) deepProtoType3.deepClone();
System.out.println(deepProtoType3.hashCode());
System.out.println(deepProtoType4.hashCode());
System.out.println(deepProtoType3.getDeepCloneTarget().hashCode());
System.out.println(deepProtoType4.getDeepCloneTarget().hashCode());
}
}
注意事项和细节
- 创建新的对象较为复杂时,可以利用原型模式简化对象的创建流程,同时也能够提高效率。
- 不用重新初始化对象,而是动态地获得对象运行时的状态。
- 如果原始对象发生变化(增加或者减少属性),其他克隆对象也会发生相应的变化,无需修改代码。
- 在实现深克隆的时候可能需要比价复杂的代码。
- 缺点:需要为每一个类配备一个克隆方法,这对全新的类来说并不是很难,但对已有的类进行改造时,需要修改其源码,违背了ocp原则。