static var isAlarming = false static var coins = 0 static var refunds = 0 static let locked = Locked() static let unlocked = Unlocked() static var state: Turnstile = locked func reset() { Turnstile.isLocked = true Turnstile.isAlarming = false Turnstile.coins = 0 Turnstile.refunds = 0 Turnstile.state = Turnstile.locked } func deposit() { Turnstile.coins += 1 } func refund() { Turnstile.refunds += 1 } func pass() { Turnstile.state.pass() } func coin() { Turnstile.state.coin() } } final class Locked: Turnstile { override func coin() { Locked.state = Locked.unlocked Locked.isLocked = false Locked.isAlarming = false deposit() } override func pass() { Locked.isAlarming = true } } final class Unlocked: Turnstile { override func coin() { refund() } override func pass() { Unlocked.isLocked = true Unlocked.state = Unlocked.unlocked } } Monostateを普通のクラスに戻すのは難しい !