รูปแบบการออกแบบ - รูปแบบต้นแบบ

รูปแบบต้นแบบหมายถึงการสร้างวัตถุที่ซ้ำกันโดยคำนึงถึงประสิทธิภาพ รูปแบบการออกแบบประเภทนี้มาจากรูปแบบการสร้างสรรค์เนื่องจากรูปแบบนี้เป็นวิธีที่ดีที่สุดวิธีหนึ่งในการสร้างวัตถุ

รูปแบบนี้เกี่ยวข้องกับการใช้อินเทอร์เฟซต้นแบบซึ่งบอกให้สร้างโคลนของวัตถุปัจจุบัน รูปแบบนี้ใช้เมื่อสร้างวัตถุโดยตรงมีค่าใช้จ่ายสูง ตัวอย่างเช่นวัตถุจะถูกสร้างขึ้นหลังจากการดำเนินการฐานข้อมูลที่มีค่าใช้จ่ายสูง เราสามารถแคชอ็อบเจ็กต์ส่งคืนโคลนในการร้องขอครั้งต่อไปและอัปเดตฐานข้อมูลเมื่อจำเป็นซึ่งจะช่วยลดการเรียกฐานข้อมูล

การนำไปใช้

เรากำลังจะสร้างระดับนามธรรมรูปร่าง และการเรียนคอนกรีตขยายรูปร่างระดับ คลาสShapeCacheถูกกำหนดให้เป็นขั้นตอนถัดไปซึ่งเก็บอ็อบเจ็กต์รูปร่างไว้ในHashtableและส่งกลับการโคลนเมื่อได้รับการร้องขอ

PrototypPatternDemoคลาสสาธิตของเราจะใช้คลาสShapeCacheเพื่อรับวัตถุShape

ขั้นตอนที่ 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.");
   }
}

ขั้นตอนที่ 3

สร้างชั้นเรียนจะได้รับการเรียนคอนกรีตจากฐานข้อมูลและเก็บไว้ใน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