ArchitectureMidMCQ

What is the Singleton pattern and what are its downsides?

Test your knowledge:

Explanation & Code

Answer: A Singleton ensures only one instance of a class exists globally. It's convenient but widely overused in iOS.

Code Example:

// Standard Swift singleton
class AnalyticsManager {
    static let shared = AnalyticsManager()
    private init() {}  // prevent external instantiation

    func track(_ event: String) { ... }
}

// Usage
AnalyticsManager.shared.track("button_tapped")

Downsides:

  • Hard to test — can't inject a mock; tests share global state
  • Hidden dependencies — callers depend on it without declaring it
  • Threading issues — shared mutable state needs synchronization
  • Tight coupling — callers are coupled to the concrete type

When it's acceptable:

  • Logging, analytics, app-level config (read-only)
  • Avoid for anything that manages data or has side effects

Rate your understanding:

Ready to practice more Architecture?

Test yourself with our interactive quiz mode or browse all curated questions for this topic.