シングルトンパターンは、クラスが唯一のインスタンスのみを持つことを保証し、それへのグローバルなアクセスポイントを提供します。正確に1つのオブジェクトが何か(設定、コネクションプール、ロガー)を調整すべき場合に使用されますが、やや議論の余地のあるパターンでもあります。
シングルトンが行うこと
SINGLETON → guarantee a class has only ONE instance, with global access to it:
→ the class controls its own instantiation (private constructor)
→ returns the SAME instance every time it's requested
→ for: things there should only be ONE of, accessed from many places
例
{
#instance;
() {
(!.#instance) {
.#instance = ();
}
.#instance;
}
}
