利用序列化和反序列化完成深复制

ByteArrayOutputStream bos=new ByteArrayOutputStream();
  ObjectOutputStream oos=new ObjectOutputStream(bos);
  oos.writeObject(s1);
  byte[] bytes=bos.toByteArray();
  ByteArrayInputStream bis=new ByteArrayInputStream(bytes);
  ObjectInputStream ois=new ObjectInputStream(bis);
  Sheep3 s3=(Sheep3) ois.readObject();

原型模式的与工厂模式相结合

工厂模式new出来的对象可以变为Clone出来的

new方式和Clone方式创建1000个对象的比较(配置低版本)

public class Test {
 public static void testNew(int size) {
  long start =System.currentTimeMillis();
  for(int i=0;i<size;i++) {
   Iphone iphone=new Iphone();
  }
  long end =System.currentTimeMillis();
  System.out.println("new耗时"+(end-start));
 }
 public static void testClone(int size) throws CloneNotSupportedException {
  long start =System.currentTimeMillis();
  Iphone iphone=new Iphone();
  for(int i=0;i<size;i++) {
   Iphone iphonex=(Iphone) iphone.clone();
  }
  long end =System.currentTimeMillis();
  System.out.println("clone耗时"+(end-start));
 }
  public static void main(String[] args) throws CloneNotSupportedException {
   testNew(1000);
   testClone(1000);
  }
}
class Iphone implements Cloneable{
 public Iphone() {
  try {
   Thread.sleep(10);//模拟new的耗时
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
 }
 @Override
 protected Object clone() throws CloneNotSupportedException {
  return super.clone();
 }
}

输出结果:new耗时16674
                   clone耗时20

最新文章

  1. Spring类型转换 ConversionSerivce Convertor
  2. NoSql系列目录
  3. c# asp.net4.0尚未在web服务器上注册
  4. Magento 新增字段的值读写丢失原因
  5. The Swift Programming Language 中文翻译版(个人翻新随时跟新)
  6. AMD加载器实现笔记(三)
  7. 将一列包含多个ID拆分多行
  8. DigitalOcean上SSH Key的创建(附DigitalOcean邀请)
  9. java call sap
  10. AngularJs赋值问题
  11. getClass 与getSimpleName
  12. vvv
  13. mysql5.7.10和mysql5.5.39两个版本对于group by函数的处理差异
  14. 201521123085 《Java程序设计》 第3周学习总结
  15. python 3.6 MJ小工具
  16. gulp将多张小图自动合成雪碧图
  17. 【Git】(1)---工作区、暂存区、版本库、远程仓库
  18. python mysql数据库操作
  19. Set实现数组去重
  20. 【Python044--魔法方法:简单定制】

热门文章

  1. C - Long Beautiful Integer codeforces 1269C 构造
  2. 详解 Properties类
  3. TeamViewer11 万全免费
  4. ASP.Net内置对象之网页之间传参(二)
  5. 用scrapy实现模拟登陆
  6. 一种特殊的生成器函数-Generator函数
  7. 手机app抓包[小米]
  8. phpstorm破解版
  9. 广深小龙-基于unittest、pytest自动化测试框架之demo来学习啦!!!
  10. Mybatis自动生成插件对数据库类型为text的处理