Created
July 9, 2016 18:05
-
-
Save wh1pch81n/dfa67ade1852cf4d10f7c2805f34def0 to your computer and use it in GitHub Desktop.
And example of using a struct singleton with only static properties. The static properties are enforced with the protocol. We can still use dependency injection which means we can do away with the "sharedInstance" construct.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| protocol Singleton3_Protocol { | |
| static var queue: OperationQueue { get } | |
| static func method1() | |
| } | |
| protocol Name { | |
| static var name: String { get set } | |
| } | |
| protocol Singleton3_Protocol2: Name { | |
| static var queue2: OperationQueue { get } | |
| static func method2() | |
| } | |
| protocol Singleton3_Protocol3: Name { | |
| static var queue3: OperationQueue { get } | |
| static func method3() | |
| } | |
| struct Singleton3: Singleton3_Protocol { | |
| static let queue = OperationQueue() | |
| static func method1() { | |
| print(1, " ", queue) | |
| } | |
| } | |
| struct SubclassSingleton3: Singleton3_Protocol, Singleton3_Protocol2, Singleton3_Protocol3 { | |
| static var name: String = "" | |
| static let queue = OperationQueue() | |
| static let queue1 = OperationQueue() | |
| static let queue2 = OperationQueue() | |
| static let queue3 = OperationQueue() | |
| static func method1() { | |
| print(2, " ", queue) | |
| print(queue1) | |
| } | |
| static func method2() { | |
| print(2, " ", queue) | |
| print(queue1) | |
| } | |
| static func method3() { | |
| print(3, " ", queue) | |
| print(queue1) | |
| } | |
| } | |
| class Weird2 { | |
| var dependency: Singleton3_Protocol.Type = Singleton3.self | |
| func bar() { | |
| dependency.method1() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment