サービスロケーターとは?
サービスロケーターは、サービスへの参照を保持し、コードが型や名前で要求したときにそれらを渡す中央のオブジェクトです。コンポーネントは依存を受け取るのではなく、必要に応じてロケーターを呼び出して取得します。これは依存性注入の代替であり、クラスが実際に何を必要としているかを隠すため、しばしばアンチパターンと見なされます。
なぜ重要か
ロケーターから依存を引き出すと、コンポーネントの本当の要件が曖昧になり、隠れたルックアップをスタブ化しなければならないためテストが複雑になります。多くのチームは、依存がシグネチャに現れる明示的な注入を好みます。
関連ガイド
What Is Inversion of Control?Inversion of control is a design style where a framework or container, rather than your own code, decides whe…
What Is Dependency Inversion?Dependency inversion is the principle that high-level modules and low-level details should both depend on abs…
What Is the Repository Pattern?The repository pattern provides a collection-like interface for accessing domain objects, hiding the details…