The Strategy Grimoire
Defining a family of algorithms, encapsulating each, and making them interchangeable during a duel.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Strategy
ARCANUM SCHOOL: Evocation // Tactics
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// FSHARP PATTERN //
// In F#, strategies are often just first-class functions
type DuelingTactic = int -> int -> string
let aggressiveTactic : DuelingTactic = fun power armor ->
sprintf "Attacking fiercely for %d damage against %d armor." (power * 2) armor
let defensiveTactic : DuelingTactic = fun power armor ->
sprintf "Striking cautiously for %d damage while maintaining %d armor." power (armor * 2)
type Duelist(power, armor) =
member _.Execute(tactic: DuelingTactic) =
printfn "%s" (tactic power armor)
let archmage = Duelist(50, 20)
printfn "Phase 1:"
archmage.Execute(aggressiveTactic)
printfn "Phase 2:"
archmage.Execute(defensiveTactic)
In the object-oriented world, Strategy requires vast hierarchies of interfaces. In F#, the Strategy pattern evaporates into the elegance of passing higher-order functions. The archmage merely swaps the tactical lambda mid-duel.
Runic Tags:
#behavioral
#strategy
#fsharp
#tactics