设计模式之五 原型模式

用原型实例指定建立对象的种类,而且经过拷贝这些原创建立新的对象。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一个性能好不少,特别要在一个循环体内
产生大量的对象时,原模型能够更好地体现其优势。 函数

逃避构造函数的约束
这既是它的优势也是缺点,直接在内存中拷贝,构造函数是不会执行的。性能

相关文章
相关标签/搜索