GOF23のデザインパターンのプロトタイプモデル02

深いコピーが完了したシリアライズとデシリアライズを使用しました

ByteArrayOutputStream BOS =新しいByteArrayOutputStream();
  ObjectOutputStreamのOOS =新しいObjectOutputStreamの(BOS);
  oos.writeObject(S1)。
  バイト[]バイト= bos.toByteArray()。
  れるByteArrayInputStreamビス=新しいれるByteArrayInputStream(バイト)。
  ObjectInputStreamのOIS =新しいObjectInputStreamの(ビス)。
  Sheep3 S3 =(Sheep3)ois.readObject();

プラントモデルと組み合わせたプロトタイプモデル

新しいオブジェクトは、クローンの外になることができ、工場のモデルのうち、

1000年、新しいオブジェクトのクローンモードを作成し、モード(低設定バージョン)を比較

パブリッククラスTest {
 パブリック静的ボイドtestNew(int型のサイズ){
  長い開始=にSystem.currentTimeMillis();
  {ため(; iはサイズ<I ++は、I = 0 INT)
   のiPhone iPhone =新しいiPhone()を。
  }
  長い端=にSystem.currentTimeMillis();
  System.out.println( "新しい耗时" +(エンドスタート));
 }
 パブリック静的ボイドtestClone(INTサイズ)CloneNotSupportedExceptionをスロー{
  長い)(=にSystem.currentTimeMillis始めます。
  iphone iphone =新しいiPhone();
  {ため(iは++; iは、サイズ<I = 0 INT)
   )(iPhone のiphonex =(iPhoneなど)iphone.cloneと、
  }
  長い端=にSystem.currentTimeMillis();
  System.out.println( "クローン耗时" +(エンドスタート));
 }
  公共の静的な無効メイン(文字列[]引数)CloneNotSupportedException {スロー
   testNew(1000)。
   testClone(1000);
  }
}
クラスiPhoneはCloneableを実装{
 公共のiPhone(){
  {試みる
   (10)のThread.sleepを; //模拟新しい的耗时
  }キャッチ(InterruptedExceptionある電子){
   e.printStackTrace();
  }
 }
 @Override
 保護オブジェクト・クローン()CloneNotSupportedException {スロー
  リターンsuper.cloneを()。
 }
}

出力:新しい時間のかかる16674
                   クローンは20になります

おすすめ

転載: www.cnblogs.com/code-fun/p/11329920.html