注意:所有文章除特别说明外,转载请注明出处.
简介
该模式是用于创建重复的对象,同时又能够保证性能。这种类型的设计模式是一种创建对象的模式。这种模式实现了一个原型接口,该接口用于创建当前对象的克隆。
    
注意:与通过对一个类进行实例化来构造新对象不同的是,原型模式是通过拷贝一个现有对象生成新对象的。浅拷贝实现 Cloneable 重写,深拷贝是通过实现 Serializable 读取二进制流。
提示:该模式一般通过一个实例进行克隆从而获得更多同一原型的实例,使用实例的clone()方法即可完成。
原型模式(深克隆和浅克隆)
浅克隆:所谓浅克隆只在类那一层进行克隆。
深克隆:在clone()方法那块儿进行clone()设置。
原型模式在JDK源码中应用
- clone() 方法
 - Cloneable 接口
 
原型模式
1.创建一个实现Cloneable接口的抽象类(原型角色)
//1.(抽象类或者接口)实现 java.lang.Cloneable 接口
public abstract class Shape implements Cloneable {
   private String id;
   protected String type;
   abstract void draw();
   public String getType(){
      return type;
   }
   public String getId() {
      return id;
   }
   public void setId(String id) {
      this.id = id;
   }
   //具体原型角色
   public Object clone() {//2.定义复制现有实例来生成新实例的方法
      Object clone = null;
      try {
         clone = super.clone();
      } catch (CloneNotSupportedException e) {
         e.printStackTrace();
      }
      return clone;
   }
}
2.创建扩展上面抽象类的实体类
public class Rectangle extends Shape {
   public Rectangle(){
     type = "Rectangle";
   }
   @Override
   public void draw() {
      System.out.println("Inside Rectangle::draw() method.");
   }
}
public class Square extends Shape {
   public Square(){
     type = "Square";
   }
   @Override
   public void draw() {
      System.out.println("Inside Square::draw() method.");
   }
}
public class Circle extends Shape {
   public Circle(){
     type = "Circle";
   }
   @Override
   public void draw() {
      System.out.println("Inside Circle::draw() method.");
   }
}
3.创建一个类 从数据库获取实体类 并把它们存储在一个 Hashtable 中 
import java.util.Hashtable;
public class ShapeCache {
    //维护一个注册表
    private static Hashtable<String, Shape> shapeMap 
      = new Hashtable<String, Shape>();
    public static Shape getShape(String shapeId) {//提供一个获取新实例的方法
      Shape cachedShape = shapeMap.get(shapeId);//提供一个找出正确实例原型的方法
      return (Shape) cachedShape.clone();//委托复制实例的方法生成新实例。
   }
   // 对每种形状都运行数据库查询,并创建该形状
   // shapeMap.put(shapeKey, shape);
   // 例如,我们要添加三种形状
   public static void loadCache() {
      Circle circle = new Circle();
      circle.setId("1");
      shapeMap.put(circle.getId(),circle);
      Square square = new Square();
      square.setId("2");
      shapeMap.put(square.getId(),square);
      Rectangle rectangle = new Rectangle();
      rectangle.setId("3");
      shapeMap.put(rectangle.getId(),rectangle);
   }
}
4.PrototypePatternDemo 使用 ShapeCache 类来获取存储在 Hashtable 中的形状的克隆
public class PrototypePatternDemo {
   public static void main(String[] args) {
      ShapeCache.loadCache();
      Shape clonedShape = (Shape) ShapeCache.getShape("1");
      System.out.println("Shape : " + clonedShape.getType());        
      Shape clonedShape2 = (Shape) ShapeCache.getShape("2");
      System.out.println("Shape : " + clonedShape2.getType());        
      Shape clonedShape3 = (Shape) ShapeCache.getShape("3");
      System.out.println("Shape : " + clonedShape3.getType());        
   }
}
总结
在原型模式中有三个角色:原型角色,定义用于复制现有实例来生成新实例的方法。 具体原型角色,实现用于复制现有实例来生成新实例的方法。 使用者角色,维护一个注册表,并提供一个找出正确实例原型的方法。最后提供一个获取新实例的方法,用来委托复制实例的方法生成新实例。
总结:对于原型模式,我们可以理解为对象的复制。在原型模式中因为复制的对象之间有差别,所以存在有深复制和浅复制。浅拷贝,其实就是引用,两者之间指向同一块内存区域。深拷贝,其实就是开辟另一块内存空间。
