定義
原型模式(Prototype Pattern)是用于創(chuàng)建重復(fù)的對(duì)象,同時(shí)又能保證性能秃症。這種類(lèi)型的設(shè)計(jì)模式屬于創(chuàng)建型模式南片,它提供了一種創(chuàng)建對(duì)象的最佳方式眼滤。
這種模式是實(shí)現(xiàn)了一個(gè)原型接口赏僧,該接口用于創(chuàng)建當(dāng)前對(duì)象的克隆矮冬。當(dāng)直接創(chuàng)建對(duì)象的代價(jià)比較大時(shí),則采用這種模式次哈。例如,一個(gè)對(duì)象需要在一個(gè)高代價(jià)的數(shù)據(jù)庫(kù)操作之后被創(chuàng)建吆录。我們可以緩存該對(duì)象窑滞,在下一個(gè)請(qǐng)求時(shí)返回它的克隆,在需要的時(shí)候更新數(shù)據(jù)庫(kù)恢筝,以此來(lái)減少數(shù)據(jù)庫(kù)調(diào)用哀卫。
實(shí)現(xiàn)
我們將創(chuàng)建一個(gè)抽象類(lèi) Shape 和擴(kuò)展了 Shape 類(lèi)的實(shí)體類(lèi)。下一步是定義類(lèi) ShapeCache撬槽,該類(lèi)把 shape 對(duì)象存儲(chǔ)在一個(gè) Hashtable 中此改,并在請(qǐng)求的時(shí)候返回它們的克隆。
PrototypPatternDemo侄柔,我們的演示類(lèi)使用 ShapeCache 類(lèi)來(lái)獲取 Shape 對(duì)象共啃。
原型模式的 UML 圖
步驟 1
創(chuàng)建一個(gè)實(shí)現(xiàn)了 Clonable 接口的抽象類(lèi)。
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
創(chuàng)建擴(kuò)展了上面抽象類(lèi)的實(shí)體類(lèi)暂题。
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
創(chuàng)建一個(gè)類(lèi)移剪,從數(shù)據(jù)庫(kù)獲取實(shí)體類(lèi),并把它們存儲(chǔ)在一個(gè) 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();
}
// 對(duì)每種形狀都運(yùn)行數(shù)據(jù)庫(kù)查詢(xún)纵苛,并創(chuàng)建該形狀
// 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 類(lèi)來(lái)獲取存儲(chǔ)在 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
驗(yàn)證輸出攻人。
Shape : Circle
Shape : Square
Shape : Rectangle