【设计模式】Java设计模式 - 原型模式
【设计模式】Java设计模式 - 原型模式
目录?? 不断学习才是王道
?? 继续踏上学习之路,学之分享笔记
?? 总有一天我也能像各位大佬一样
??原创作品,更多关注我CSDN: 一个有梦有戏的人
??准备将博客园、CSDN一起记录分享自己的学习心得!!!
??分享学习心得,欢迎指正,大家一起学习成长!
- https://blog.csdn.net/qq_34748010/article/details/124917704
方式二:通过对象序列化
通过对象流的方式将对象进行序列化之后在进行反序列化完成深拷贝。这样的效果会更好。
只要在Car类中添加一个deepClone方法// 方式二:实现序列化 public Object deepClone() { // 创建流对象 ByteArrayOutputStream bos = null; ObjectOutputStream oos = null; ByteArrayInputStream bis = null; ObjectInputStream ois = null; try { // 序列化 bos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(bos); oos.writeObject(this); // 把当前对象以对象流的方式输出 //反序列 bis = new ByteArrayInputStream(bos.toByteArray()); ois = new ObjectInputStream(bis); Car car = (Car) ois.readObject(); return car; } catch (Exception e) { e.printStackTrace(); return null; } finally { // 关闭流 try { ois.close(); bis.close(); oos.close(); bos.close(); } catch (IOException e) { e.printStackTrace(); } } }测试:只要调用deepClone方法就可以
Car c1 = (Car) car.deepClone(); Car c2 = (Car) car.deepClone();最后实验结果:

??今天的内容不少!创作不易,如有错误请指正,感谢观看!记得一键三连哦!????德德小建议:
理解设计模式不是一件简单的事情,需要不断的学习和动手去练习,才能理解。只有掌握好设计模式,才能够真正的理解SpringAOP和Mybatis的底层原理。各位读者可以和我一样,动手敲一敲代码,甚至用不同的例子来做,通过debug一步一步调试,还有就是多看看别人的例子。能够有助于理解!谢谢各位观看指点!?? ?? ??