美文网首页
第六章 原型模式(Prototype Pattern)

第六章 原型模式(Prototype Pattern)

作者: 穹生变 | 来源:发表于2019-06-23 18:25 被阅读0次

    注意事项:与通过对一个类进行实例化来构造新对象不同的是,原型模式是通过拷贝一个现有对象生成新对象的。浅拷贝实现 Cloneable,重写,深拷贝是通过实现 Serializable 读取二进制流。

    1.简介

    • 一般在初始化信息不变得情况下,克隆是最好得办法。这既隐藏了对象创建得细节,又对性能是大大得提高。
    • 原型模式其实就是从一个对象再创建另一个可定制得对象,而且不需要知道任何创建得细节。

    2.场景

    1、在写简历得时候名称等一些信息不需要修改,但是工作经历等需要修改,重新初始化一个对象显得非常低效和麻烦。
    2、如果查询一些数据得时候会很耗时,这时可以将查询出来得对象作为原型,以后每次查询都用这个对象得克隆对象作为返回信息,这样会提高效率。

    3.优缺点

    优点:

    1、性能提高。
    2、逃避构造函数的约束。

    缺点:

    1、配备克隆方法需要对类的功能进行通盘考虑,这对于全新的类不是很难,但对于已有的类不一定很容易,特别当一个类引用不支持串行化的间接对象,或者引用含有循环结构的时候。。
    2、必须实现 Cloneable 接口。

    4. 图片模型

    image.png

    5. 代码实现

    1. 创建一个实现了 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() {
          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());        
       }
    }
    

    相关文章

      网友评论

          本文标题:第六章 原型模式(Prototype Pattern)

          本文链接:https://www.haomeiwen.com/subject/ogoiqctx.html