原型模式是属于建立型模式的一种,是经过拷贝原型对象来建立新的对象.html
万能的Java超类Object提供了clone()方法来实现对象的拷贝.java
能够在如下场景中使用原型模式:api
使用原型实例指定要建立的对象类型,并经过拷贝这个原型来建立新对象。安全
一. 浅拷贝和深拷贝的概念oracle
Object.clone()
方法实现的是对象的浅拷贝, 所谓浅拷贝就是当对象中有复杂引用类型的域变量时, 只拷贝该域变量的引用而不是内容, 当有任一方法修改域变量的状态时会同时影响原型对象及拷贝对象, 实际上他们共用了同一个堆内存. 深拷贝建立的对象便是对原对象的彻底拷贝,对任一对象的操做不会影响其余对象的状态.函数
java中提供了Cloneable接口, 约定实现接口Cloneable
且重写Object.clone()
方法的类能够用来拷贝自身. Cloneable
是一个标记接口, 其中没有定义任何方法.性能
二. 下面的代码演示了使用clone()方法实现的深拷贝,这种方式更适合用于比较简单的对象,不然clone()方法的实现可能会变得异常复杂.this
import lombok.AllArgsConstructor; import lombok.Data; @Data @AllArgsConstructor public class CarProperty implements Cloneable { private String power; private double maxSpeed; private double oilPerKm; public Object clone(){ Object obj = null; try { obj = super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return obj; } }
import lombok.AllArgsConstructor; import lombok.Data; @Data @AllArgsConstructor public class Car implements Cloneable { private String brand; private double price; private CarProperty carProperty; /** * 深拷贝在此实现,对于复杂的应用类型, 这里的代码可能会至关复杂,若是类有修改(新增成员变量等),这里也须要相应修改 * @return */ public Object clone(){ Object car = null; try { car = super.clone(); CarProperty carPropertyClone = (CarProperty)this.getCarProperty().clone(); ((Car)car).setCarProperty(carPropertyClone); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return car; } public static void main(String[] args) { CarProperty carProperty = new CarProperty("8匹",250,30); Car car= new Car("BMW",200,carProperty); Car copy = (Car) car.clone(); System.out.println("copy最大速度为: "+copy.getCarProperty().getMaxSpeed()); System.out.println("原型最大速度为: "+car.getCarProperty().getMaxSpeed()); car.getCarProperty().setMaxSpeed(360); System.out.println("copy最大速度为: "+copy.getCarProperty().getMaxSpeed()); System.out.println("原型最大速度为: "+car.getCarProperty().getMaxSpeed()); } }
三. 深拷贝的其余实现方式: 除了上面的方法,还能够使用反射机制建立对象的深拷贝, 另一种更简单的方式是使用序列化;
下面的代码使用序列化方式实现对象的深拷贝,需实现Serializable
接口.code
import java.io.*; public class DeepCloneBase implements Serializable { public Object deepClone() { ByteArrayOutputStream byteArrayOutputStream = null; ObjectOutputStream objectOutputStream = null; ByteArrayInputStream byteArrayInputStream = 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 objectInputStream.readObject(); } catch (Exception e) { e.printStackTrace(); } finally { try { byteArrayOutputStream.close(); objectOutputStream.close(); byteArrayInputStream.close(); objectInputStream.close(); } catch (Exception e) { e.printStackTrace(); } } return null; } }
import lombok.AllArgsConstructor; import lombok.Data; import java.io.Serializable; @Data @AllArgsConstructor public class MyCar extends DeepCloneBase { private String brand; private double price; private CarProperty carProperty; public static void main(String[] args) throws Exception{ // 注意CarProperty也须要实现Serializable接口,代码再也不单独列出 CarProperty carProperty = new CarProperty("8匹",250,30); MyCar car= new MyCar("BMW",200,carProperty); MyCar copy = (MyCar)car.deepClone(); if (copy!=null){ System.out.println("copy最大速度为: "+copy.getCarProperty().getMaxSpeed()); System.out.println("原型最大速度为: "+car.getCarProperty().getMaxSpeed()); car.getCarProperty().setMaxSpeed(360); System.out.println("copy最大速度为: "+copy.getCarProperty().getMaxSpeed()); System.out.println("原型最大速度为: "+car.getCarProperty().getMaxSpeed()); }else{ System.out.println("对象没拷贝成功...."); } } }
优势: 1. 若是对象建立比较复杂, 能够简化建立过程, 提升效率;2. 能够保留对象状态;
缺点: 对于clone()方式,若是类有修改则须要修改clone()的实现,不符合开闭原则; 复杂对象的clone逻辑可能较复杂;htm