Service Locator pattern in Swift
问题 I'm interested in a flexible universal Service Locator design pattern implementation in Swift. A naive approach may be as follows: // Services declaration protocol S1 { func f1() -> String } protocol S2 { func f2() -> String } // Service Locator declaration // Type-safe and completely rigid. protocol ServiceLocator { var s1: S1? { get } var s2: S2? { get } } final class NaiveServiceLocator: ServiceLocator { var s1: S1? var s2: S2? } // Services imlementation class S1Impl: S1 { func f1() ->