GOF23设计模式——原型模式(通过系列化克隆的深克隆)
2018-08-16 02:58
495 查看
/** * 测试对象 * * @author Administrator * */ public class Name implements Cloneable, Serializable{ private static final long serialVersionUID = 1L; private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public Name clone() { try { return (Name)super.clone(); } catch (CloneNotSupportedException e) { return null; } } }
/** * 深克隆 * * @author Administrator * */ public class ProductByDeepClone implements Cloneable, Serializable{ private static final long serialVersionUID = 1L; private String id; private Name name; private String[] part; public ProductByDeepClone(String id, Name name, String[] part) { super(); this.id = id; this.name = name; this.part = part; } public String getId() { return id; } public void setId(String id) { this.id = id; } public Name getName() { return name; } public void setName(Name name) { this.name = name; } public String[] getPart() { return part; } public void setPart(String[] part) { this.part = part; } /** * 深克隆 */ public ProductByDeepClone clone() { try { ProductByDeepClone p = (ProductByDeepClone)super.clone(); p.name = this.name.clone(); p.part = this.part.clone(); return p; } catch (CloneNotSupportedException e) { e.printStackTrace(); return null; } } }
ublic class ClientBySerializable { public static void main(String[] args) throws IOException, ClassNotFoundException { String id = "1"; Name name = new Name(); name.setName("name1"); String[] part = { "part1", "part2" }; ProductByDeepClone p1 = new ProductByDeepClone(id, name, part); System.out.println("首次赋值"); System.out.println(p1); System.out.println(p1.getId()); System.out.println(p1.getName().getName()); System.out.println(p1.getPart()[0]); System.out.println(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(p1); byte[] bytes = bos.toByteArray(); ByteArrayInputStream bis = new ByteArrayInputStream(bytes); ObjectInputStream ois = new ObjectInputStream(bis); ProductByDeepClone p2 = (ProductByDeepClone) ois.readObject(); // 克隆 System.out.println("克隆后的值"); System.out.println(p2); System.out.println(p2.getId()); System.out.println(p2.getName().getName()); System.out.println(p2.getPart()[0]); System.out.println(); id = "2"; name.setName("name2"); part[0] = "part21"; // 给原对象重新赋值后原先的对象的值 System.out.println("给原对象重新赋值后原先的对象的值"); System.out.println(p1); System.out.println(p1.getId()); System.out.println(p1.getName().getName()); System.out.println(p1.getPart()[0]); System.out.println(); System.out.println("给原对象重新赋值后新的对象的值"); System.out.println(p2); System.out.println(p2.getId()); System.out.println(p2.getName().getName()); System.out.println(p2.getPart()[0]); System.out.println(); } }
运行结果:
首次赋值 com.suntefan.model.prototype.ProductByDeepClone@7852e922 1 name1 part1 克隆后的值 com.suntefan.model.prototype.ProductByDeepClone@568db2f2 1 name1 part1 给原对象重新赋值后原先的对象的值 com.suntefan.model.prototype.ProductByDeepClone@7852e922 1 name2 part21 给原对象重新赋值后新的对象的值 com.suntefan.model.prototype.ProductByDeepClone@568db2f2 1 name1 part1阅读更多
相关文章推荐
- GOF 23 设计模式之 原型模式(prototype) 深复制与浅复制举例
- 【GOF23设计模式】_原型模式JAVA233-234
- GOF23设计模式之原型模式之实现
- 【GOF23设计模式】原型模式
- 233_尚学堂_高淇_java300集最全视频教程_【GOF23设计模式】_原型模式_prototype_浅复制_深复制_反序列化实现深复制
- 23中设计模式之_原型模式(深/浅拷贝)
- 设计模式GOF23之单例模式效率测试与反射和反序列化问题的解决
- 【GOF23设计模式】_策略模式_CRM中报价策略_GUI编程中布局管理器底层架构JAVA248
- GOF 23设计模式的Visio版UML图
- 设计模式GOF23——抽象工厂模式
- 【GOF23设计模式】_外观模式_公司注册流程_迪米特法则JAVA241
- 【GOF23设计模式】状态模式
- GOF23设计模式之策略模式与实现
- 【设计模式】对象的克隆-原型模式
- (GOF23设计模式)_简单工厂模式详解
- GOF 23设计模式
- Rhyme/Java 设计模式之原型模式prototype通过clone方法和反序列化实现深复制
- GOF 23设计模式之 享元模式(FlyWeight)
- GOF23设计模式之 命令模式command
- 【GOF23设计模式】桥接模式