単一例同時使用しない場合:
公共の 密封された クラスのシングルトン { プライベート 静的シングルトンl_singleton。 パブリック 静的シングルトンGetSingleton() { 場合(l_singleton == NULL ) { l_singleton = 新しいシングルトン()。 } 戻りl_singleton。 } }
同時がある状況下では、次のように単独の例を使用することができます。
公共の 密封された クラスのシングルトン { プライベート 静的シングルトンl_singleton。 プライベート 読み取り専用 の静的 オブジェクト l_obj = 新しい オブジェクト(); パブリック 静的シングルトンGetSingleton() { 場合(l_singleton == NULL ) { ロック(l_obj) { 場合(l_singleton == NULL ) { l_singleton = 新しいシングルトン()。 } } } 戻りl_singleton。 } }