Badoo Bohdan Orlov Dec 5, 2017

Singleton Service Locator Pattern and Testing in iOS

Article Summary

Bohdan Orlov tackles one of iOS development's most controversial debates: when Singletons and Service Locators are actually useful (and when they'll wreck your test suite).

This article examines why Singletons and Service Locator patterns are often labeled anti-patterns in iOS development. Orlov provides practical guidance on using these patterns while maintaining testable code, with specific Swift implementation examples.

Key Takeaways

Critical Insight

Use Singletons sparingly based on domain modeling and inject Service Locators explicitly through factories to keep your iOS code testable.

The article reveals a specific pledge developers can make about static variables that fundamentally changes how you architect iOS apps.

About This Article

Problem

iOS developers have a hard time testing code that uses singletons like UIApplication.shared and UIDevice.current. These global objects make network requests and change state in ways that can't be controlled during unit tests.

Solution

Bohdan Orlov suggests wrapping singleton functionality behind protocols, such as ImageDownloading, and passing them in as dependencies instead of accessing static instances directly.

Impact

When you pass dependencies through factories instead of using static variables, developers get compile-time errors if dependencies change. This catches problems early and prevents hidden failures across thousands of tests.