定義
用于创建重复的对象,同时又能保证性能。它属于创建型设计模式,它提供了一种创建对象的最佳方法。
テンプレート
プロトタイプ(試作品)
の新しいインスタンスを生成するために、既存のインスタンスを複製するための方法を定義するための責任が製品の役割。サンプルプログラムでは、製品のインタフェースは、この役割を果たしています。
ConcretePrototype(特定のプロトタイプ)
ConcretePrototypeの役割は、既存のインスタンスと新しいインスタンスを複製実装する責任があります。サンプルプログラムでは、メッセージボックスクラスとUnderlinePenのように、この役割を再生します。
クライアント(ユーザー)
複製されたインスタンスを使用するために新しいインスタンスを生成するための責任がクライアントの役割。サンプルプログラムでは、マネージャークラスがこの役割を果たしています。
例
- 抽象クラスの形状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;
}
}
- 特定のカテゴリの定義(ConcretePrototype)
//Rectangle
public class Rectangle extends Shape {
public Rectangle(){
type = "Rectangle";
}
@Override
public void draw() {
System.out.println("Inside Rectangle::draw() method.");
}
}
//Square
public class Square extends Shape {
public Square(){
type = "Square";
}
@Override
public void draw() {
System.out.println("Inside Square::draw() method.");
}
}
//Circle
public class Circle extends Shape {
public Circle(){
type = "Circle";
}
@Override
public void draw() {
System.out.println("Inside Circle::draw() method.");
}
}
- (クライアントに相当)の管理
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);
}
}
- テスト手順
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
利点
該当シーンは、実際にその利点を下回っています
短所
- コードの複雑さを読み取る複数のクラスを作成します(すべてのデザインパターン共通の問題)
- 5つの基本型タイプに加えて、ちょうどシャローコピークローン方法、単にない再作成メンバ変数、実装のニーズに合わせてカスタムクローン法の基準点です。
- コンストラクタメソッドは、クラスのクローンによって呼び出されていない、いくつかのシーンは、(いくつかの初期化作業のコンストラクタを行う)、追加の処理が必要となります
該当シーン
(1)オブジェクトの広い範囲、それらはクラスに統合することができない場合
(2)時間のクラスをインスタンス生成することは困難である
例を生成するあまりに複雑なプロセスを、一般的に、クラスに応じてインスタンスを生成することは困難であり、ユーザが生成したい前まったく同じインスタンスがウォークスルーを介して作成されたとき、我々は動作例は、それを作成し、必要なときにコピーして、新しいインスタンスを生成することによって、それらを保存するために、ユーザーを進めます。
(3)は、デカップリングフレームと生成されたインスタンスはときだと思う
フレームは、インスタンスを生成するインスタンス「のプロトタイプを登録」する前に、そのインスタンスを作成するために、クラス名を指定することはできません、特定のクラスに依存して、コピーされません。この例では、新しいインスタンスを生成します。
その他の例:
追加します