设计模式之原型模式

来源:互联网 发布:薪酬数据分析哪些内容 编辑:程序博客网 时间:2024/06/07 23:35

PROTOTYPE—跟MM用QQ聊天,一定要说些深情的话语了,我搜集了好多肉麻的情话,需要时只要copy出来放到QQ里面就行了,这就是我的情话prototype了。

意思是:可以通过这个原型对象复制出更多的对象出来。

public class ProtoType{

private static long serialVersionUID =1l;

  private String string;

  private SerializableObject obj;

//浅复制

public Object Clone() throws CloneNotSupportedException{

Prototype prototype =Prototype (super.clone());

returnprototype ;

}


//深复制

public Object deepclone() throws IOException,ClassNotFoundException {

//写入当前对象

ByteArrayOutputStream bos=new ByteArrayOutputStream ();

ObjectOutputStream oos=new ObjectOutputStream(bos);

oos.write(this);

//读取二进制流中产生的对象

ByteArrayInputStream bis=new ByteArrayInputStream();

ObjectInputStream ois=new ObjectInputStream(bis);

return ois.readObject();

}

}





原创粉丝点击