Skip to content

Commit 7f4d75d

Browse files
Tavernariochococo
authored andcommitted
Add monostate example
1 parent b8a9cba commit 7f4d75d

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

Design-Patterns.playground.zip

14 KB
Binary file not shown.

Design-Patterns.playground/Pages/Creational.xcplaygroundpage/Contents.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,43 @@ CurrencyFactory.currency(for: .greece)?.code ?? noCurrencyCode
192192
CurrencyFactory.currency(for: .spain)?.code ?? noCurrencyCode
193193
CurrencyFactory.currency(for: .unitedStates)?.code ?? noCurrencyCode
194194
CurrencyFactory.currency(for: .uk)?.code ?? noCurrencyCode
195+
/*:
196+
🔂 Monostate
197+
------------
198+
199+
The monostate pattern is a alternativa to singleton, so in that case monostate still
200+
the state as static instead of all object as singleton. You can use a protocol to apply
201+
dependency inversion helping you on unit tests.
202+
203+
### Example:
204+
*/
205+
struct Settings {
206+
207+
enum Theme {
208+
case .old
209+
case .new
210+
}
211+
212+
private static var theme: Theme
213+
214+
var currentTheme: Theme {
215+
get { Settings.theme }
216+
set(newTheme) { Settings.theme = newTheme }
217+
}
218+
}
219+
/*:
220+
### Usage:
221+
*/
222+
223+
// When change the theme
224+
let settings = Settings() // Starts using theme .old
225+
settings.theme = .new // Change theme to .new
226+
227+
//On Screen 1
228+
let screenColor: Color = Settings().theme == .old ? .gray : .white
229+
230+
//On Screen 2
231+
let screenTitle: String = Settings().theme == .old ? "Itunes Connect" : "App Store Connect"
195232
/*:
196233
🃏 Prototype
197234
------------

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,47 @@ CurrencyFactory.currency(for: .greece)?.code ?? noCurrencyCode
10021002
CurrencyFactory.currency(for: .spain)?.code ?? noCurrencyCode
10031003
CurrencyFactory.currency(for: .unitedStates)?.code ?? noCurrencyCode
10041004
CurrencyFactory.currency(for: .uk)?.code ?? noCurrencyCode
1005+
```
1006+
1007+
🔂 Monostate
1008+
------------
1009+
1010+
The monostate pattern is a alternativa to singleton, so in that case monostate still
1011+
the state as static instead of all object as singleton. You can use a protocol to apply
1012+
dependency inversion helping you on unit tests.
1013+
1014+
### Example:
1015+
1016+
```swift
1017+
struct Settings {
1018+
1019+
enum Theme {
1020+
case .old
1021+
case .new
1022+
}
1023+
1024+
private static var theme: Theme
1025+
1026+
var currentTheme: Theme {
1027+
get { Settings.theme }
1028+
set(newTheme) { Settings.theme = newTheme }
1029+
}
1030+
}
1031+
```
1032+
1033+
### Usage:
1034+
1035+
```swift
1036+
1037+
// When change the theme
1038+
let settings = Settings() // Starts using theme .old
1039+
settings.theme = .new // Change theme to .new
1040+
1041+
//On Screen 1
1042+
let screenColor: Color = Settings().theme == .old ? .gray : .white
1043+
1044+
//On Screen 2
1045+
let screenTitle: String = Settings().theme == .old ? "Itunes Connect" : "App Store Connect"
10051046
```
10061047

10071048
🃏 Prototype

source/creational/monostate.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*:
2+
🔂 Monostate
3+
------------
4+
5+
The monostate pattern is a alternativa to singleton, so in that case monostate still
6+
the state as static instead of all object as singleton. You can use a protocol to apply
7+
dependency inversion helping you on unit tests.
8+
9+
### Example:
10+
*/
11+
struct Settings {
12+
13+
enum Theme {
14+
case .old
15+
case .new
16+
}
17+
18+
private static var theme: Theme
19+
20+
var currentTheme: Theme {
21+
get { Settings.theme }
22+
set(newTheme) { Settings.theme = newTheme }
23+
}
24+
}
25+
/*:
26+
### Usage:
27+
*/
28+
29+
// When change the theme
30+
let settings = Settings() // Starts using theme .old
31+
settings.theme = .new // Change theme to .new
32+
33+
//On Screen 1
34+
let screenColor: Color = Settings().theme == .old ? .gray : .white
35+
36+
//On Screen 2
37+
let screenTitle: String = Settings().theme == .old ? "Itunes Connect" : "App Store Connect"

0 commit comments

Comments
 (0)