Badoo Bohdan Orlov

Badoo's Singleton Service Locator Pattern and Testing in iOS

Original post 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.

Continue reading

What This Article Covers

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.

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

This page summarizes “Singleton Service Locator Pattern and Testing in iOS” by Badoo, published Dec 5, 2017. All credit for the original work goes to the authors.

Read the original on medium.com (opens in new tab)

AI tools for iOS

Browse all AI tools →