软件设计模式之原型模式

软件设计模式之原型模式

此模式涉及实现原型接口,该接口告诉您创建当前对象的克隆

技术开发 编程 技术框架 技术发展

 

软件设计模式之原型模式

此模式涉及实现原型接口,该接口告诉您创建当前对象的克隆

原型模式是指在考虑性能的同时创建重复的对象。这种设计模式属于创建模式,因为该模式提供了创建对象的最佳方法之一。此模式涉及实现原型接口,该接口告诉您创建当前对象的克隆。当直接创建对象的成本很高时,使用此模式。例如,要在进行昂贵的数据库操作之后创建一个对象。我们可以缓存对象,在下一个请求时返回其克隆,并在需要时更新数据库,从而减少数据库调用。

实作

我们将创建一个抽象类Shape 和扩展Shape类的具体类。下一步将ShapeCache类定义为将形状对象存储在Hashtable中并在需要时返回其副本的下一步。

PrototypPatternDemo,我们的演示类将使用ShapeCache类来获取Shape对象。

prototype_pattern_uml_diagram.jpg

第1步

创建一个实现Clonable接口的抽象类。


Shape.java


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步

创建扩展上述类的具体类。


Rectangle.java


public class Rectangle extends Shape {


   public Rectangle(){

     type = "Rectangle";

   }


   @Override

   public void draw() {

      System.out.println("Inside Rectangle::draw() method.");

   }

}

Square.java


public class Square extends Shape {


   public Square(){

     type = "Square";

   }


   @Override

   public void draw() {

      System.out.println("Inside Square::draw() method.");

   }

}

Circle.java


public class Circle extends Shape {


   public Circle(){

     type = "Circle";

   }


   @Override

   public void draw() {

      System.out.println("Inside Circle::draw() method.");

   }

}

第三步

创建一个类以从数据库中获取具体的类并将其存储在Hashtable中。


ShapeCache.java


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();

   }


   // for each shape run database query and create shape

   // shapeMap.put(shapeKey, shape);

   // for example, we are adding three shapes

   

   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中的形状的克隆 。


PrototypePatternDemo.java


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());

   }

}

第5步

验证输出。


Shape : Circle

Shape : Square

Shape : Rectangle

技术开发 编程 技术框架 技术发展