public class Client { public static void main(String[] args) { Sheep sheep = new Sheep("tom", 1, "白色"); Sheep sheep2 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor()); Sheep sheep3 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor()); } }
1)在创建新的对象时,总是需要重新获取原始对象的属性,如果创建的对象比较复杂,则效率较低
2)总是需要重新初始化对象,而不是动态的获取对象运行时的状态,不够灵活
1)原型模式是指:用原型实例指定创建对象的种类,并且通过拷贝这些原型,创建新的对象
2)原型模式是一种创建型的设计模式,允许一个对象再创建另一个可定制的都西昂,无需知道创建的细节
3)工作原理: 对象.clone()
重写对象的clone() 方法
/** * @author houChen * @date 2020/9/24 7:00 * @Description: 克隆羊问题 */ 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 + ‘\‘‘ + ‘}‘; } // 克隆该实例,使用默认的克隆方法来完成 @Override protected Object clone() throws CloneNotSupportedException { Sheep sheep = null; try { sheep = (Sheep)super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return sheep; } } public class Client { public static void main(String[] args) throws CloneNotSupportedException { System.out.println("使用原型模式完成对象的创建!============="); Sheep sheep = new Sheep("tom", 1, "白色"); Sheep sheep1 = (Sheep)sheep.clone(); System.out.println(sheep1); } }
1)对于数据类型是基本数据类型的成员变量,浅拷贝会直接进行值传递,也就是将属性值复制一份给新的对象
2)对于数据类型是引用类型的成员变量,比如说成员变量是某个类的对象,那么浅拷贝会进行引用传递,就是将成员变量的引用值复制一份给新的对象。所以两个对象的成员变量会指向同一个实例。在这个情况下,修改一个对象的成员变量会影响到另一个对象的成员变量
1)复制对象的基本数据类型的成员变量
2)复制每个引用数据类型的成员变量
3)深拷贝的实现方式
方式1:重写 clone方法,实现深拷贝
方式2:通过对象序列化实现深拷贝
方式1:重写clone方法实现深拷贝
public class DeepProtoType implements Serializable,Cloneable { public String name; public DeepClonableTarget target; public DeepProtoType() { } // 深拷贝 - 方式一 使用clone方法 @Override protected Object clone() throws CloneNotSupportedException { Object deep = null; // 这里完成对属性中基本类型和字符串的克隆 deep = super.clone(); // 对引用类型的属性进行单独处理 DeepProtoType deepProtoType = (DeepProtoType) deep; deepProtoType.target = (DeepClonableTarget)target.clone(); return deepProtoType; } } public class Client { public static void main(String[] args) throws CloneNotSupportedException { DeepProtoType p = new DeepProtoType(); p.name = "宋江"; p.target = new DeepClonableTarget("及时雨","class"); //方式1 完成深拷贝 DeepProtoType p2 = (DeepProtoType)p.clone(); System.out.println("p.target"+p.target.hashCode()); System.out.println("p2.target"+p2.target.hashCode()); } }
方式2:使用对象的序列化实现深拷贝
public class DeepProtoType implements Serializable,Cloneable { public String name; public DeepClonableTarget target; public DeepProtoType() { } //深拷贝 - 方式二 通过对象的序列化实现(推荐) public Object deepClone(){ ByteArrayOutputStream bos = null; ObjectOutputStream oos = null; ByteArrayInputStream bis= 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 copy= (DeepProtoType)ois.readObject(); return copy; } catch (Exception e) { e.printStackTrace(); return null; }finally { try { bos.close(); ; oos.close(); bis.close(); ois.close(); } catch (IOException e) { e.printStackTrace(); } } } } public class Client { public static void main(String[] args) throws CloneNotSupportedException { DeepProtoType p = new DeepProtoType(); p.name = "宋江"; p.target = new DeepClonableTarget("及时雨","class"); //方式2 使用序列化,反序列化完成深拷贝 DeepProtoType p3 = (DeepProtoType)p.clone(); System.out.println("p.target"+p.target.hashCode()); System.out.println("p3.target"+p3.target.hashCode()); } }
原文:https://www.cnblogs.com/houchen/p/13752821.html