Java Prototype Pattern(原型模式)

原型模式(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)用。

優(yōu)點(diǎn): 1、性能提高。 2、逃避構(gòu)造函數(shù)的約束。
缺點(diǎn): 1、配備克隆方法需要對(duì)類(lèi)的功能進(jìn)行通盤(pán)考慮,這對(duì)于全新的類(lèi)不是很難,但對(duì)于已有的類(lèi)不一定很容易,特別當(dāng)一個(gè)類(lèi)引用不支持串行化的間接對(duì)象,或者引用含有循環(huán)結(jié)構(gòu)的時(shí)候。 2、必須實(shí)現(xiàn) Cloneable 接口。 3、逃避構(gòu)造函數(shù)的約束。

注意事項(xiàng):與通過(guò)對(duì)一個(gè)類(lèi)進(jìn)行實(shí)例化來(lái)構(gòu)造新對(duì)象不同的是,原型模式是通過(guò)拷貝一個(gè)現(xiàn)有對(duì)象生成新對(duì)象的。淺拷貝實(shí)現(xiàn) Cloneable,重寫(xiě),深拷貝是通過(guò)實(shí)現(xiàn) Serializable 讀取二進(jìn)制流。

  1. 創(chuàng)建一個(gè)實(shí)現(xiàn)了 Clonable 接口的抽象類(lèi)。
/**
 * 1. 創(chuàng)建一個(gè)實(shí)現(xiàn)了 Clonable 接口的抽象類(lèi)。
 * @author mazaiting
 */
public abstract class Shape implements Cloneable{
    
    private String id;
    protected String type;
    
    public abstract void draw();
    
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getType() {
        return type;
    }
    
    /**
     * 克隆
     */
    public Object clone(){
        Object clone = null;
        try {
            clone = super.clone();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return clone;   
    }
    
}
  1. 創(chuàng)建擴(kuò)展了上面抽象類(lèi)的實(shí)體類(lèi)。
/**
 * 2. 創(chuàng)建擴(kuò)展了上面抽象類(lèi)的實(shí)體類(lèi)。
 * @author mazaiting
 */
public class Circle extends Shape{

    public Circle(){
        type = "circle";
    }
    
    @Override
    public void draw() {
        System.out.println("circle draw()");        
    }

}

/**
 * 2. 創(chuàng)建擴(kuò)展了上面抽象類(lèi)的實(shí)體類(lèi)。
 * @author mazaiting
 */
public class Rectangle extends Shape{

    public Rectangle(){
        type = "rectangle";
    }
    
    @Override
    public void draw() {
        System.out.println("rectangle draw()");     
    }

}

/**
 * 2. 創(chuàng)建擴(kuò)展了上面抽象類(lèi)的實(shí)體類(lèi)。
 * @author mazaiting
 */
public class Square extends Shape{

    public Square(){
        type = "square";
    }
    
    @Override
    public void draw() {
        System.out.println("square draw()");        
    }

}
  1. 創(chuàng)建一個(gè)類(lèi),從數(shù)據(jù)庫(kù)獲取實(shí)體類(lèi),并把它們存儲(chǔ)在一個(gè) Hashtable 中。
/**
 * 3. 創(chuàng)建一個(gè)類(lèi),從數(shù)據(jù)庫(kù)獲取實(shí)體類(lèi),并把它們存儲(chǔ)在一個(gè) Hashtable 中。
 * @author mazaiting
 */
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();
    }
    
    /**
     * 添加數(shù)據(jù),創(chuàng)建該形狀
     */
    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);     
    }
    
}
  1. 主函數(shù)調(diào)用
public class Client {
    public static void main(String[] args) {
        // 加載數(shù)據(jù)
        ShapeCache.loadCache();
        
        // 獲取克隆實(shí)例
        Shape shape = ShapeCache.getShape("1");
        // 后去類(lèi)型
        System.out.println(shape.getType());
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容