SwiftPumpkin

13 of 22 · System architecture

Services vs Singletons

A singleton is a type with one globally reachable instance (static let shared). A service is a capability behind a protocol that's passed to whoever needs it. You can have exactly one instance of a service without making it global.

How it's wired

.sharedInjectedConsumerHidden dependencySingleton.shared, global stateConsumerDeclared dependencyService protocolInjected instance
calls or tellsreports backOne instance is fine. Reaching for it globally is the problem: hidden coupling and shared test state.
  • Singletons are global access to one instance.
  • A service is a dependency behind a protocol, passed in.
PartOwnsLooks like
SingletonOne global instancestatic let shared
ServiceCapability behind a protocolprotocol ThanksCounting
ScopeWho owns the instance's lifetimeApp, user session, screen

Why .shared hurts at scale

  • Hidden dependencies: a function that calls .shared doesn't say so in its signature. You find out when a test leaks state.
  • Test pollution: shared state survives between tests and makes them order-dependent. Parallel test execution makes it worse.
  • Lifetime: a singleton lives forever. Clearing a global cache at logout can race a background save, and user A's data lands in user B's session. Scope user data to a session object that is dropped on logout.
  • Migration doesn't need a rewrite: add a protocol, conform the singleton, inject it with a default argument, then move call sites one at a time.

When a singleton is fine

System-owned services at the edgesFileManager.default. URLSession.shared is fine for simple fetches, but it shares cookies, cache and credentials, so give user-scoped traffic its own configured session.
Anything holding user dataSession-scoped, injected.

Compared

Singleton vs injected service vs session scope

SingletonInjectedSession scope
Testable in isolationNoYesYes
LifetimeProcessOwner's choiceLogin to logout
Multiple accountsBreaksPossibleNatural

Expert notes

  • One instance is fine. Global access is the problem.

Check yourself

Answer out loud before you reveal. Recall is what makes it stick.

30-second answer

Explain Services vs Singletons in 30 seconds.

System designAsked directly

Singletons versus injected services: what goes wrong with UserManager.shared, and how do you migrate?

iOS codingAsked indirectly

Tests pass individually but fail together because one test logs a user in. What's the root cause, and the fix?

System designAsked indirectly

Some resources should be shared, like an image cache. How do you share them without a singleton's downsides?

iOS codingAsked indirectly

On logout you clear the image-cache singleton, but user A's thumbnails still show up for user B. What happened?

System designAsked indirectly

Product now wants two accounts signed in at once, and SessionManager.shared is used everywhere. How do you plan the migration?

System designAsked directly

The AppDelegate holds the API client, the cache and the current user. What test decides what may stay global?

Read it in the app's source