序列化实现原型模式
序列化实现原型模式
在 Prototype 类中实现深度克隆(序列化与反序列化),实现 Cloneable,Serializable两接口。
package gupa.design.prototype; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class Prototype implements Cloneable,Serializable{ private String name; private PrototypeBo pb; public String getName() { return name; } public void setName(String name) { this.name = name; } public PrototypeBo getPb() { return pb; } public void setPb(PrototypeBo pb) { this.pb = pb; } public Prototype(){} public Prototype(String name, PrototypeBo pb) { super(); this.name = name; this.pb = pb; } @Override public String toString() { return "Prototype [name=" + name + ", pb=" + pb + "]"; } public Object clone() throws CloneNotSupportedException { return this.deepclone(); } public Object deepclone(){ Prototype copy = null; try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(this); ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bis); copy = (Prototype)ois.readObject(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return copy; } }
在引用对象上实现 Serializable 接口就行了。
package gupa.design.prototype; import java.io.Serializable; public class PrototypeBo implements Serializable{ private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public PrototypeBo(){} public PrototypeBo(String name) { super(); this.name = name; } @Override public String toString() { return "PrototypeBo [name=" + name + "]"; } // public Object clone() { // PrototypeBo pb = null; // try { // ByteArrayOutputStream bos = new ByteArrayOutputStream(); // ObjectOutputStream oos = new ObjectOutputStream(bos); // oos.writeObject(this); // // ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); // ObjectInputStream ois = new ObjectInputStream(bis); // pb = (PrototypeBo)ois.readObject(); // } catch (IOException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } catch (ClassNotFoundException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // return pb; // } }
测试
package gupa.design.prototype; public class test { public static void main(String[] args) { Prototype p = new Prototype(); p.setName("aaa"); PrototypeBo pb = new PrototypeBo("abc"); p.setPb(pb); Prototype p1 = null; try { p1 = (Prototype)p.clone(); } catch (CloneNotSupportedException e) { // TODO Auto-generated catch block e.printStackTrace(); } // PrototypeBo pb1 = (PrototypeBo)pb.clone(); System.out.println(p==p1); System.out.println(p.getPb()==p1.getPb()); System.out.println(p.hashCode()+"-----"+p1.hashCode()); System.out.println(p.getPb().hashCode()+"-----"+p1.getPb().hashCode()); } }
实现效果:
false false 1118140819-----1078694789 2101973421-----1831932724