原型模式 现在有一只羊,姓名为:tom,年龄为:1,颜色为:白色,请创建和tom羊属性完全相同的10只羊。
1 2 3 4 5 6 7 @Data @AllArgsConstructor public class Sheep { private String name; private int age; private String color; }
传统的方式的优缺点:
1.基本介绍
原型模式(Prototype模式)是指:用原型实例指定创建对象的种类,并且通过拷贝这些原型,创建新的对象
原型模式是一种创建型设计模式,允许一个对象再创建另外一个可定制的对象,无需知道如何创建的细节
工作原理是 :通过将一个原型对象传给那个要发动创建的对象,这个要发动创建的对象通过请求原型对象拷贝它们自己来实施创建,即对象.clone()
1)Prototype:原型类,声明一个克隆自己的接口
2) ConcretePrototype:具体的原型类,实现一个克隆自己的操作
3)Client:让一个原型对象克隆自己,从而创建一个新的对象(属性一样)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 @Data @AllArgsConstructor public class Sheep implements Cloneable { private String name; private int age; private String color; @Override protected Object clone () { Sheep sheep = null ; try { sheep = (Sheep) super .clone(); }catch (Exception e){ System.out.println(e.getMessage()); } return sheep; } }
1 2 3 4 5 6 7 8 9 10 11 public class Client { public static void main (String[] args) { Sheep sheep = new Sheep("tom" ,1 ,"白色" ); Sheep sheep1 = (Sheep) sheep.clone(); Sheep sheep2 = (Sheep) sheep.clone(); System.out.println(sheep1); System.out.println(sheep2); } }
2.原型模式在Spring中的使用 spring创建bean的时候使用prototype参数来创建一个bean
3.深入讨论-浅拷贝和深拷贝 浅拷贝 当羊属性中有一个对象的时候,会发什么
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 @Data public class Sheep implements Cloneable { private String name; private int age; private String color; private Sheep friend; public Sheep (String name, int age, String color) { this .name = name; this .age = age; this .color = color; } @Override protected Object clone () { Sheep sheep = null ; try { sheep = (Sheep) super .clone(); }catch (Exception e){ System.out.println(e.getMessage()); } return sheep; } }
1 2 3 4 5 6 7 8 9 10 public static void main (String[] args) { Sheep sheep = new Sheep("tom" ,1 ,"白色" ); sheep.setFriend(new Sheep("jack" ,2 ,"黑色" )); Sheep sheep1 = (Sheep) sheep.clone(); Sheep sheep2 = (Sheep) sheep.clone(); System.out.println(sheep1); System.out.println(sheep2); }
对于数据类型是基本数据类型 的成员变量,浅拷贝会直接进行值传递 ,也就是将该属性值复制一份给新的对象。
对于数据类型是引用数据类型 的成员变量,比如说成员变量是某个数组、某个类的对象 等,那么浅拷贝会进行引用传递 ,也就是只是将该成员变量的引用值(内存地址)复制一份给新的对象。 因为实际上两个对象的该成员变量都指向同一个实例。在这种情况下,在一个对象中修改该成员变量会影响到另一个对象的该成员变量值
前面我们克隆羊就是浅拷贝
浅拷贝是使用默认的clone()方法来实现 sheep = (Sheep) super.clone();
深拷贝
复制对象的所有基本数据类型的成员变量值
为所有引用数据类型的成员变量申请存储空间,并复制每个引用数据类型成员变量所引用的对象,直到该对象可达的所有对象。也就是说,对象进行深拷贝要对整个对象(包括对象的引用类型)进行拷贝
深拷贝实现方式1:重写clone方法来实现深拷贝
深拷贝实现方式2:通过对象序列化实现深拷贝(推荐)
clone方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public class DeepCloneableTarget implements Serializable ,Cloneable { private static final long serialVersionUID = 1L ; 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(); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 @NoArgsConstructor public class DeepProtoType implements Serializable , Cloneable { public String name; public DeepCloneableTarget deepCloneableTarget; @Override protected Object clone () throws CloneNotSupportedException { Object deep = null ; deep = super .clone(); DeepProtoType deepProtoType = (DeepProtoType)deep; deepProtoType.deepCloneableTarget = (DeepCloneableTarget) deepCloneableTarget.clone(); return deepProtoType; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class Client { public static void main (String[] args) throws Exception { DeepProtoType d = new DeepProtoType(); d.name = "宋江" ; d.deepCloneableTarget = new DeepCloneableTarget("大牛" ,"大牛的类" ); DeepProtoType d2 = (DeepProtoType) d.clone(); System.out.println(d.deepCloneableTarget.hashCode()); System.out.println(d2.deepCloneableTarget.hashCode()); } }
通过序列号
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 @NoArgsConstructor public class DeepProtoType implements Serializable , Cloneable { public String name; public DeepCloneableTarget deepCloneableTarget; 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 copyObject = (DeepProtoType) ois.readObject(); return copyObject; }catch (Exception e){ e.printStackTrace(); return null ; }finally { try { ois.close(); bis.close(); oos.close(); bos.close(); } catch (IOException e) { e.printStackTrace(); } } } }
1 2 3 4 5 6 7 8 9 10 11 12 public class Client { public static void main (String[] args) throws Exception { DeepProtoType d = new DeepProtoType(); d.name = "宋江" ; d.deepCloneableTarget = new DeepCloneableTarget("大牛" ,"大牛的类" ); DeepProtoType d2 = (DeepProtoType) d.deepClone(); System.out.println(d.name+"********" +d.deepCloneableTarget.hashCode()); System.out.println(d.name+"********" +d2.deepCloneableTarget.hashCode()); } }
4.总结
创建新的对象比较复杂时,可以利用原型模式简化对象的创建过程 ,同时也能够提高效率
不用重新初始化对象,而是动态地获得对象运行时的状态
如果原始对象发生变化(增加或者减少属性),其它克隆对象的也会发生相应的变化,无需修改代码
在实现深克隆的时候可能需要比较复杂的代码
缺点:需要为每一个类配备一个克隆方法 ,这对全新的类来说不是很难,但对已有的类进行改造时,需要修改其源代码,违背了ocp原则,这点请注意.