ノートを読んでJavaのデザインパターン[3] - プロトタイプモデル

入門

第三モードプロトタイプのJavaデザインパターンは、我々は通常、クラスのインスタンスを作成するために、クラス名を指定するには、新しいキーワードを使用しますが、我々は時々の「前提のクラス名を指定せずにインスタンスを生成するための」ニーズが発生しました。この時、私たちのプロトタイプモードによって、あなたは、インスタンスのコピーを作成するために、Cloneableインタフェースを使用してのcloneメソッドを使用することができます。

実現

まず最初に、私たちのコピーを容易にするために、cloneメソッドを持つ抽象クラスを作成する必要があります

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;
   }
}

次のステップは、具体的なエンティティクラスに比較的単純なコピー上記抄録を維持することです


public class Rectangle extends Shape {
   public Rectangle(){
     type = "Rectangle";
   }
   public void draw() {
      System.out.println("Inside Rectangle::draw() method.");
   }
}
public class Square extends Shape {
   public Square(){
     type = "Square";
   }
   public void draw() {
      System.out.println("Inside Square::draw() method.");
   }
}
public class Circle extends Shape {
   public Circle(){
     type = "Circle";
   }
   public void draw() {
      System.out.println("Inside Circle::draw() method.");
   }
}

次に、これらのエンティティクラスのキャリアとして、ハッシュテーブルの全体の存在を新しいクラスを作成することです

package test;
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();
   }
   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);
   }
}

最後のステップは、エンティティのこれらの異なるクラスの実装の呼び出しです


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

出力:

Shape : Circle
Shape : Square
Shape : Rectangle

導入のメリットとデメリット

  1. 利点は、
    (1)新たなパターンの新しいインスタンスを作成提案用いずに、実行時の性能を改善するため
    のコンストラクタの制約を回避するために、(2)
  2. 欠点
    (1)クローン方法があることができる実装しなければならない
    (2)単一のレプリケーション・フォーマットを、それらの柔軟性は特に良好ではありません

注意事項

Prototypeパターンは、オブジェクトのインスタンス作成の新しいクラスに直接ではなく、すでに存在するオブジェクトをコピーして、新しいオブジェクトをコピーするために、従来と異なっています

概要

これは、新しい読書ノート、およびインターネットへの教訓、最初の待機を行い、待機を参照し、表示されません

リリース8元の記事 ウォンの賞賛2 ビュー122

おすすめ

転載: blog.csdn.net/weixin_43917604/article/details/104556274