设计模式之原型模式
一、基础介绍
1.1什么是原型模式?
原型模式(Prototype模式)是指:用原型实例指定创建对象的种类,并且通过拷 贝这些原型,创建新的对象 原型模式是一种创建型设计模式,允许一个对象再创建另外一个可定制的对象, 无需知道如何创建的细节 工作原理是:通过将一个原型对象传给那个要发动创建的对象,这个要发动创建 的对象通过请求原型对象拷贝它们自己来实施创建,即 对象.clone() 4) 形象的理解:孙大圣拔出猴毛, 变出其它孙大圣
1.2原型模式注意事项
创建新的对象比较复杂时,可以利用原型模式简化对象的创建过程,同时也能够提 高效率 不用重新初始化对象,而是动态地获得对象运行时的状态 如果原始对象发生变化(增加或者减少属性),其它克隆对象的也会发生相应的变化, 无需修改代码 在实现深克隆的时候可能需要比较复杂的代码 缺点:需要为每一个类配备一个克隆方法,这对全新的类来说不是很难,但对已有 的类进行改造时,需要修改其源代码,违背了ocp原则.
1.3常见的原型模式实现
浅拷贝:进行简单数据类型的拷贝,引用的内部类对象不进行拷贝,还是指向原对象 深拷贝:进行简单数据类型的拷贝,且引用的内部类对象也进行拷贝,生成真正的类对象的克隆。
二、浅克隆
2.1浅拷贝详情介绍
对于数据类型是基本数据类型的成员变量,浅拷贝会直接进行值传递,也就是将 该属性值复制一份给新的对象。 对于数据类型是引用数据类型的成员变量,比如说成员变量是某个数组、某个类 的对象等,那么浅拷贝会进行引用传递,也就是只是将该成员变量的引用值(内 存地址)复制一份给新的对象。因为实际上两个对象的该成员变量都指向同一个 实例。在这种情况下,在一个对象中修改该成员变量会影响到另一个对象的该成 员变量值 前面我们克隆羊就是浅拷贝 浅拷贝是使用默认的 clone()方法来实现 sheep = (Sheep) super.clone();
2.2代码例子1(常规克隆)
此方法简单易懂,但是当类对象成员逐渐添加时,就会显得十分的麻烦了。
1)类对象
public class Sheep { private String name ; private String color ; private int age ; public Sheep(String name, String color, int age) { this . name = name; this . color = color; this . age = age; } @Override public String toString() { return "Sheep{" + "name='" + name + ' \' ' + ", color='" + color + ' \' ' + ", age=" + age + '}' ; } public String getName() { return name ; } public void setName(String name) { this . name = name; } public String getColor() { return color ; } public void setColor(String color) { this . color = color; } public int getAge() { return age ; } public void setAge( int age) { this . age = age; } }
2)实现测试类
public class SheepTest { public static void main(String[] args) { Sheep s1= new Sheep( "tom" , "red" , 2 ); Sheep s2= new Sheep(s1.getName(),s1.getColor(),s1.getAge()); Sheep s3= new Sheep(s1.getName(),s1.getColor(),s1.getAge()); Sheep s4= new Sheep(s1.getName(),s1.getColor(),s1.getAge()); Sheep s5= new Sheep(s1.getName(),s1.getColor(),s1.getAge()); System. out .println(s1); System. out .println(s2); System. out .println(s3); System. out .println(s4); System. out .println(s5); } }
2.3代码例子2(clone方法克隆)
clone方法,相对于第一种的方法的扩展性更加好,使用起来也更加的方便。但是其需要先实现cloneable接口,其次在重写父类Object的clone方法,最后通过clone方法进行对象的克隆。
1)类对象
public class Sheep implements Cloneable{ private String name ; private String color ; private int age ; public Sheep(String name, String color, int age) { this . name = name; this . color = color; this . age = age; } @Override public String toString() { return "Sheep{" + "name='" + name + ' \' ' + ", color='" + color + ' \' ' + ", age=" + age + '}' ; } public String getName() { return name ; } public void setName(String name) { this . name = name; } public String getColor() { return color ; } public void setColor(String color) { this . color = color; } public int getAge() { return age ; } public void setAge( int age) { this . age = age; } /** * 此方法是实现类对象的克隆 * 1. 使用前需要实现 cloneable 接口 * 2. 使用时需要捕获异常 * @return * @throws CloneNotSupportedException */ @Override protected Object clone(){ // 创建 Sheep 对象 Sheep sheep= null ; // 通过使用 Object 对象的 clone 属性,进行克隆 try { sheep=(Sheep) super .clone(); } catch (Exception e){ e.printStackTrace(); } return sheep; } }
2)实现测试类
public class SheepTest { public static void main(String[] args) { Sheep s1= new Sheep( "tom" , "red" , 2 ); Sheep s2= (Sheep) s1.clone(); Sheep s3= (Sheep) s1.clone(); Sheep s4= (Sheep) s1.clone(); Sheep s5= (Sheep) s1.clone(); System. out .println(s1); System. out .println(s2); System. out .println(s3); System. out .println(s4); System. out .println(s5); } }
三、深拷贝
3.1深拷贝详情介绍
复制对象的所有基本数据类型的成员变量值 为所有引用数据类型的成员变量申请存储空间,并复制每个引用数据类型成员变 量所引用的对象,直到该对象可达的所有对象。也就是说,对象进行深拷贝要对 整个对象进行拷贝 深拷贝实现方式1:重写clone方法来实现深拷贝 深拷贝实现方式2:通过对象序列化实现深拷贝(推荐)
3.2重写clone方式
重写clone的方式其实是,通过当前实体中的内部实体类对象,进行各自的clone处理,最后再将处理好的clone设置于当前实体类中。此方法,内部实体类多时,用起来特别的不方便。
1)实体类对象Dog
public class Dog implements Cloneable{ private String name ; private int age ; 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 Dog(String name, int age) { this . name = name; this . age = age; } @Override public String toString() { return "Dog{" + "name='" + name + ' \' ' + ", age=" + age + '}' ; } @Override protected Object clone() throws CloneNotSupportedException { return super .clone(); } }
2)实体类对象Sheep
public class Sheep implements Cloneable{ private String name ; private Dog friend ; public Sheep(String name, Dog friend) { this . name = name; this . friend = friend; } public String getName() { return name ; } public void setName(String name) { this . name = name; } public Dog getFriend() { return friend ; } public void setFriend(Dog friend) { this . friend = friend; } @Override public String toString() { return "Sheep{" + "name='" + name + ' \' ' + ", friend=" + friend + '}' ; } @Override protected Object clone() { // 1. 创建类对象 Sheep sheep= null ; try { // 2. 将当前类进行克隆 sheep= (Sheep) super .clone(); // 3. 将当前类中的内对象进行克隆 sheep. friend = (Dog) friend .clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return sheep; } }
3)测试类
public class SheepTest { public static void main(String[] args) { Sheep s1= new Sheep( "tom" , new Dog( "tony" , 2 )); Sheep s2= (Sheep) s1.clone(); Sheep s3= (Sheep) s1.clone(); Sheep s4= (Sheep) s1.clone(); Sheep s5= (Sheep) s1.clone(); System. out .println(s1+ "dog of hashcode" +s1.getFriend().hashCode()); System. out .println(s2+ "dog of hashcode" +s2.getFriend().hashCode()); System. out .println(s3+ "dog of hashcode" +s3.getFriend().hashCode()); System. out .println(s4+ "dog of hashcode" +s4.getFriend().hashCode()); System. out .println(s5+ "dog of hashcode" +s5.getFriend().hashCode()); } }
3.3序列化方式
序列化方法,是通过java对于序列化时的特殊处理,达到的clone效果。其为我们所推荐的方法。
1)实体类对象Dog
public class Dog implements Serializable{ private final static long serialVersionUID = 1L ; private String name ; private int age ; 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 Dog(String name, int age) { this . name = name; this . age = age; } @Override public String toString() { return "Dog{" + "name='" + name + ' \' ' + ", age=" + age + '}' ; } }
2)实体类对象Sheep
public class Sheep implements Serializable { private String name ; private Dog friend ; public Sheep(String name, Dog friend) { this . name = name; this . friend = friend; } public String getName() { return name ; } public void setName(String name) { this . name = name; } public Dog getFriend() { return friend ; } public void setFriend(Dog friend) { this . friend = friend; } @Override public String toString() { return "Sheep{" + "name='" + name + ' \' ' + ", friend=" + friend + '}' ; } 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); Sheep sheep= (Sheep) ois.readObject(); return sheep; } catch (Exception e){ e.printStackTrace(); } finally { try { bis.close(); bos.close(); ois.close(); oos.close(); } catch (IOException e) { e.printStackTrace(); } } return null ; } }
3)测试类对象
public class SheepTest { public static void main(String[] args) { Sheep s1= new Sheep( "tom" , new Dog( "tony" , 2 )); Sheep s2= (Sheep) s1.deepClone(); System. out .println(s1+ "dog of hashcode" +s1.getFriend().hashCode()); System. out .println(s2+ "dog of hashcode" +s2.getFriend().hashCode()); } }