Last active
April 18, 2025 16:39
-
-
Save podratz/8d65c5307e9bfaf63fb2b3674e46a729 to your computer and use it in GitHub Desktop.
Strategy Selector
This file contains 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
class StrategySelector { | |
private var strategy: (() -> Void)! | |
private var state: String = "" | |
init() { | |
strategy = strategyA | |
} | |
func execute() { | |
strategy() | |
} | |
func read() -> String { | |
state | |
} | |
func selectStrategyA() { | |
strategy = strategyA | |
} | |
func selectStrategyB() { | |
strategy = strategyB | |
} | |
private func strategyA() { | |
state.append("A") | |
} | |
private func strategyB() { | |
state.append("B") | |
} | |
} | |
let selector = StrategySelector() | |
selector.execute() | |
selector.selectStrategyB() | |
selector.execute() | |
selector.selectStrategyA() | |
selector.execute() | |
selector.execute() | |
selector.read() // ABAA |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment