The Service Locator Pattern in Swift

Avoid Dependency Injection entirely!

Steven Curtis
2 min readOct 2, 2020
Photo by QuickOrder on Unsplash

The service locator pattern is designed to encapsulate the processes invovled in obtaining a service with a strong layer of abstraction.

Prerequisites:

The explanation

Service locator has a central registry known as the service locator, that on request returns the data required to perform a task. The service locator returns instances of services when they are requested by the service consumers or service clients.

The Service Locator pattern does not describe how to instantiate serveices, rather it describes a way to register services and locate them.

The Example

Here is an example. This example is written with a Playground in mind: In fact the tests are included (and will…

--

--