创建型模式---原型模式

来源:互联网 发布:知我者,二三子 编辑:程序博客网 时间:2024/05/02 02:51

一、优缺点

        原型模式的优点在于:创建对象的时候没有用new方式,而是直接是对内存二进制流的拷贝,相对于new方式,性能更优,
     
原型模式的缺点在于:直接对内存二进制流的拷贝少了约束。

二、实现

     

public class YuanXingMode {/** * 原型模式的优点在于:创建对象的时候没有用new方式,而是直接是对内存二进制流的拷贝,相对于new方式,性能更优, * 原型模式的缺点在于:直接对内存二进制流的拷贝少了约束。 */public static void main(String[] args) {Prototype p = new Prototype("a");Prototype pp = (Prototype) p.clone();System.out.println(p.name);System.out.println(pp.name);}}class Prototype implements Cloneable {String name;public Prototype(String name) {this.name = name;}@Overridepublic Object clone() {try {return super.clone();} catch (CloneNotSupportedException e) {e.printStackTrace();return null;}}}


 


 

原创粉丝点击