The Strategy Stance
Swap arcane algorithms on the fly depending on the weakness of the entity you are battling.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Strategy
ARCANUM SCHOOL: Divination // Tactics
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// SCALA PATTERN //
type AttackStrategy = (Int) => Int
val fireBlast: AttackStrategy = power => power * 2
val frostNova: AttackStrategy = power => power + 10
class BattleMage(var strategy: AttackStrategy) {
def cast(basePower: Int): Int = strategy(basePower)
}
// Usage:
// val mage = new BattleMage(fireBlast)
// println(mage.cast(10)) // 20
// mage.strategy = frostNova
In Scala, functions are first-class citizens. The Strategy pattern is radically simplified by merely passing different function values representing the algorithms.
Runic Tags:
#scala
#behavioral
#tactics
#functions-as-objects