Skip to content

Service Locator

A pattern that gathers access to shared services into a central registry to reduce coupling at the call site.

  • When referencing audio, save, or analytics services globally.
  • When you want to simplify DI in the early stage of development.
  • Service Interface
  • Locator Registry
  • Bootstrap Registration

The code below is a simplified Unity example based on the scenario described above.

public interface IAudioService
{
void PlaySfx(string clipId);
}
public static class GameServices
{
public static IAudioService AudioService { get; private set; }
public static void RegisterAudioService(IAudioService audioService)
{
AudioService = audioService;
}
}
public sealed class DamageFeedbackSystem
{
public void OnHit()
{
GameServices.AudioService?.PlaySfx("Hit");
}
}
  • Shared services can be replaced or injected from one place, which speeds up early development.
  • Since callers no longer own creation responsibilities, usage code becomes simpler.
  • Hidden global dependencies make test isolation and dependency tracking harder.
  • If initialization order is wrong, runtime null-reference errors can easily occur.

This shows the flow where the client looks up a service by interface key and uses it.

Service Locator Flow