-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathSingleton.swift
156 lines (110 loc) · 3.23 KB
/
Singleton.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//
// Singleton.swift
// ios-design-patterns
//
// Created by Astemir Eleev on 22/04/2018.
// Copyright © 2018 Astemir Eleev. All rights reserved.
//
import Foundation
public class Singleton {
// MARK: - Singleton property
public static let instnage = Singleton()
// MARK: - Properties
public private(set) var counter: Int
// MARK: - Private initializer
private init() {
// Private initialier ensires that there will not access it from the outside, so instances can only be created inside the class
counter = 0
}
// MARK: - Methods
public func foo() {
print(#function + " foo")
}
public func bar() {
print(#function + " bar")
}
public func increment() {
counter += 1
}
public func decrement() {
counter -= 1
}
}
public class StructSignleton {
// MARK: - Sharead instance class property
public class var sharedInstance: StructSignleton {
struct Static {
static let instance = StructSignleton()
}
return Static.instance
}
// MARK: - Properties
public private(set) var counter: Int
// MARK: - Initializers
private init() {
counter = 0
}
// MARK: - Methods
public func foo() {
print(#function + " foo")
}
public func bar() {
print(#function + " bar")
}
public func increment() {
counter += 1
}
public func decrement() {
counter -= 1
}
}
public class DispatchSingleton {
// MARK: - Thread safe shared instance
public class var sharedInstance: DispatchSingleton {
struct Static {
static var onceToken: String = "DispatchSingleton"
static var instance: DispatchSingleton? = nil
}
DispatchQueue.once(token: Static.onceToken) {
Static.instance = DispatchSingleton()
}
return Static.instance!
}
// MARK: - Properties
public private(set) var counter: Int
// MARK: - Initializers
private init() {
counter = 0
}
// MARK: - Methods
public func foo() {
print(#function + " foo")
}
public func bar() {
print(#function + " bar")
}
public func increment() {
counter += 1
}
public func decrement() {
counter -= 1
}
}
public extension DispatchQueue {
// MARK: - Properties
private static var _onceTracker = [String]()
// MARK: - Methods
/// Executes a block of code, associated with a unique token, only once. The code is thread safe and will onle execute the code once even in the presence of multithreaded calls.
///
/// - Parameters:
/// - token: is a unique reverse DNS-style name such as io.eleev.astemir or a GUID
/// - block: is a non-escaping closure that is executed only once
class func once(token: String, block: () -> Void ) {
objc_sync_enter(self); defer { objc_sync_exit(self) }
if _onceTracker.contains(token) {
return
}
_onceTracker.append(token)
block()
}
}