Browse Questions
  • The Factory pattern creates objects without exposing the creation logic.
  • SOLID is a set of five design guidelines for maintainable OOP code: | Principle | Idea | Swift application | |--|--|--| | Single Responsibility | A type should have one reason to change | Split a…
  • Both static and class define type-level properties and methods, but static members cannot be overridden by subclasses (final), whereas class members allow dynamic dispatch and can be overridden.
  • OSLog is Apple's structured logging framework.

Answer: The Factory pattern creates objects without exposing the creation logic. The caller asks for an object and gets one back — without knowing which concrete type was created.

Code Example:

// Simple factory function
enum Environment { case dev, staging, production }

struct NetworkClientFactory {
    static func make(for environment: Environment) -> NetworkClientProtocol {
        switch environment {
        case .dev:
            return MockNetworkClient()
        case .staging:
            return NetworkClient(baseURL: "https://staging.api.com")
        case .production:
            return NetworkClient(baseURL: "https://api.com")
        }
    }
}

// Caller doesn't care which type comes back
let client = NetworkClientFactory.make(for: .production)

// Factory method on a protocol
protocol ViewControllerFactory {
    func makeLoginViewController() -> UIViewController
    func makeHomeViewController() -> UIViewController
}