設計模式之五 原型模式

用原型實例指定建立對象的種類,而且經過拷貝這些原創建立新的對象。ide

image

public class PrototypeClass  implements  Cloneable{

    @Override
    protected Object clone() {
        // TODO Auto-generated method stub
        PrototypeClass  prototypeClass = null;
        
        try {
            prototypeClass = (PrototypeClass) super.clone();
        } catch (CloneNotSupportedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return prototypeClass;
    }

    
}

原型模式的優勢:
性能優良:
原型模式是內存二進制流的拷貝,要比直接new一個性能好不少,特別要在一個循環體內
產生大量的對象時,原模型能夠更好地體現其優勢。 函數

逃避構造函數的約束
這既是它的優勢也是缺點,直接在內存中拷貝,構造函數是不會執行的。性能

相關文章
相關標籤/搜索