The Strategy Hex
Swappable algorithms for dynamic spell weaving.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Strategy
ARCANUM SCHOOL: Evocation // Tactics
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// KOTLIN PATTERN //
typealias AttackStrategy = (Int) -> Int
val fireAttack: AttackStrategy = { baseDamage -> baseDamage * 2 }
val frostAttack: AttackStrategy = { baseDamage -> baseDamage + 5 }
class ElementalMage(var strategy: AttackStrategy) {
fun strike(baseDamage: Int) {
println("Damage dealt: ${strategy(baseDamage)}")
}
}
The Strategy Hex
Combat is fluid; rigid algorithms fail. The Strategy pattern defines a family of algorithms, encapsulating each, and making them interchangeable. In Kotlin, thanks to higher-order functions, we can often bypass massive class hierarchies entirely, passing simple lambda expressions to represent our tactical permutations.
Runic Tags:
#kotlin
#behavioral
#strategy