December 2018
Intermediate to advanced
414 pages
10h 19m
English
Each state will be responsible for its own logic and transitions, so we can start moving the logic into each state:
struct UnknownState: CardReaderState { static let shared = UnknownState() func perform(context: CardReader) { // Perform local initialization, When everything is ready // Toggle the state to Waiting context.state = WaitingState.shared }}
As you can see, this is cleaner, as our initialization states are encapsulated. In the example code following, the rest of the logic is even more encapsulated:
struct WaitingState: CardReaderState { static let shared = WaitingState() func perform(context: CardReader) { guard seenCard() else { return } // we have seen a card! context.state ...Read now
Unlock full access