Created
May 20, 2020 06:27
-
-
Save menangen/e3caa45ea72afc7b51f1ef5348917ee2 to your computer and use it in GitHub Desktop.
Monostate Pattern in Swift
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
import Foundation | |
class Cat { var name = "Kisa" } | |
let cat = Cat() | |
class State { | |
static var instance = cat | |
} | |
class Mono: State { | |
var catName: String { | |
return State.instance.name | |
} | |
func check() { | |
let stateName = State.instance.name | |
let thisName = self.catName | |
assert(stateName == thisName) | |
print(thisName) | |
} | |
} | |
let monoCat = Mono() | |
monoCat.check() | |
State.instance.name = "Pusa" | |
monoCat.check() | |
cat.name = "Boris" | |
monoCat.check() | |
Mono.instance.name = "Sena" | |
monoCat.check() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment