Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
重複の可能性: シングルトンの何がそんなに悪いのですか? シングルトン設計パターン: 落とし穴
よく耳にしますが、明確な理由が見つかりません。
Avoid the singleton anti-pattern and replace it with DI.
しかし、なぜ?
ステートフル シングルトンは単体テストがはるかに困難です。
問題がないと思われるステートレスシングルトンを使用しています。
シングルトンはインターフェースを実装できるため、依存性注入を使用して渡すことができます (可能であれば、そのように渡す必要があります)。