Service Locator と DI どちらを使うか、という話。

Service Locator とは?

Create a service locator that contains references to the services and that encapsulates the logic that locates them. In your classes, use the service locator to obtain service instances

サービスへの参照を含み、それらを見つけるロジックをカプセル化するサービスロケーターを作成します。 クラスからは、サービスロケータを使用してサービスインスタンスを取得します。

The Service Locator Pattern | Microsoft Docs

どちらがどうなのか?

The choice between Service Locator and Dependency Injection is less important than the principle of separating service configuration from the use of services within an application.

Service Locator と Dependency Injection のどちらを選択するかは、アプリケーション内でサービスの設定とそれの利用を分離するという原則よりも重要ではありません。

Inversion of Control Containers and the Dependency Injection pattern

Service location is not an anti-pattern. There are anti-patterns that involve use of a service locator along with other similar constructs. There are anti patterns that involve the use of DI. Most devises we use in programming involve both (virtuous) patterns, and anti-patterns, which is really just a grand way of saying pros and cons. Generally speaking people who summarise the world in terms of only pros or only cons are said to be engaging in splitting.

Service Locator 自体はアンチパターンではありません。 他の要素と共に Service Locator を使用するときにアンチパターンが存在します。 DI を利用する場合にもアンチパターンがあります。 私たちがプログラミングで使用するほとんどの案は、良いパターンと悪いパターンの両方を含みます。 一般的に言えば、長所のみまたは短所のみの観点から要約する人々は、分割に従事していると言われています。

Splitting (also called black and white thinking or all-or-nothing thinking) is the failure in a person’s thinking to bring together both positive and negative qualities of the self and others into a cohesive, realistic whole. It is a common defense mechanism used by many people. The individual tends to think in extremes (i.e., an individual’s actions and motivations are all good or all bad with no middle ground.)

分割(白黒思考またはオールオアナッシング思考とも呼ばれます)は、自己と他者の肯定的な面と否定的な面の両方の性質をまとまりのある現実的な全体にまとめることができないことです。 それは多くの人々によって使用されている一般的な防御メカニズムです。 個人は極端に考える傾向があります(すなわち、個人の行動や動機はすべて悪い、またはすべてが悪いこともありません)。

I need to take a look about and see what discussions there may be on the subject of polarised views and whether they are more prevalent among programmers than other professions.

私は偏極的な見解に関してどのような議論ができるのか、またそれらについて議論します。 他の職業よりもプログラマーの間で流行しています。

Service locator vs dependency injection. | Weird Scenes Inside The Goldmine

Dependency injection is all the rage now on Android but there are also some other patterns worth considering for managing dependencies. A Service Locator is a very simple pattern which can be implemented in few lines (50?) of code and can sometimes be a viable alternative or complement to DI frameworks like Dagger 2.

依存性注入は現在Android上で大流行していますが、依存性を管理するために考慮する価値のある他のパターンもいくつかあります。 Service Locatorは、ほんの数行(50?)のコードで実装できる非常に単純なパターンであり、Dagger 2のようなDIフレームワークに代わる実行可能な代替手段である場合もあります。

The Service Locator is considered by some (or many) as an anti-pattern, but I would argue that you always have to choose the right tool for the job (and it’s good to have a variety of tools in your toolbox). In some cases the Service Locator can be an extremely simple and efficient solution. It can also get handy in case you have constraints like APK size, method count, build speed or overall complexity.

Service Locatorは、一部(または多数)によってアンチパターンと見なされていますが、仕事に適したツールを常に選択する必要があると思います(ツールボックスにさまざまなツールを用意しておくことをお勧めします)。 場合によっては、Service Locatorは非常に単純で効率的なソリューションになります。 APKサイズ、メソッド数、ビルド速度、全体的な複雑さなどの制約がある場合にも便利です。

Service Locator pattern in Android - INLOOPX - Medium

‘Dependency Injection’ is only one form of the D in the SOLID design principles, ie. Dependency Inversion. I do highly recommend Dependency Inversion in any application that has a need to interact with dynamic data, or that has any level of business logic. Both Service Locator and Dependency Injection can be an anti-pattern or a viable pattern depending on the scenario.

「依存性注入」は、SOLID設計原則におけるDの1つの形式にすぎません。 依存関係の反転 動的データとやり取りする必要があるアプリケーション、またはあらゆるレベルのビジネスロジックを持つアプリケーションでは、Dependency Inversionを強くお勧めします。 シナリオに応じて、Service LocatorとDependency Injectionの両方をアンチパターンまたは実行可能パターンにすることができます。

Dependency Injection is over Hyped – Candor Developer

まとめ

「Service Locator」は、「シンプルなDI」てなイメージでいようと思います。

しかし、Dagger2を使うほうが後々便利なのかもしれません。

👉 Service Locator is an Anti-Pattern 


関連ワード:  AndroidGoogleおすすめ今さら聞けない開発