设计模式之五 原型模式

用原型实例指定创建对象的种类,并且通过拷贝这些原创创建新的对象。

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一个性能好很多,特别要在一个循环体内
产生大量的对象时,原模型可以更好地体现其优点。

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

原文地址:https://www.cnblogs.com/tjxwg/p/4070714.html