デザインパターン - シングルトンコード

パッケージsingeton。


インポートにjava.security.SecureRandom;

/ **
* @authorゼロ
2019年8月13日@since *。
*説明:
* /
publicクラスHungrySingleton {
プライベート静的最終HungrySingletonシングルトン=新しいHungrySingleton()。
民間最終int型のID =新しいのSecureRandom()nextInt();

プライベートHungrySingleton(){
}

パブリック静的HungrySingleton getSingleton(){
戻りシングルトン。
}

公共のint型のdoSomething(){
//するSystem.out.println(+ ID + "私はHungrySingetonよ" "!");
IDを返します。
}
}

パッケージsingeton。

インポートにjava.security.SecureRandom;

/ **
* @authorゼロ
2019年8月13日@since *。
*説明:
* /
publicクラスLazySingleton {
プライベート静的LazySingleton singeton = NULL;
民間最終int型のID =新しいのSecureRandom()nextInt();

プライベートLazySingleton(){
}

パブリック静的同期LazySingleton getSingleton(){
IF(singeton == NULL){
singeton =新しいLazySingleton()。
}
戻りsingeton。
}

公共のint型のdoSomething(){
//するSystem.out.println(+ ID + "私はLazySingetonよ" "!");
IDを返します。
}

}

おすすめ

転載: www.cnblogs.com/DeskZero/p/11707388.html