The Strategy
Swapping algorithms via higher-order functions.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Strategy
ARCANUM SCHOOL: Divination // Tactical Forethought
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// GLEAM PATTERN //
pub type AttackStrategy = fn(Int) -> Int
pub fn aggressive_stance(power: Int) -> Int { power * 2 }
pub fn defensive_stance(power: Int) -> Int { power / 2 }
pub fn execute_attack(power: Int, strategy: AttackStrategy) -> Int {
strategy(power)
}
The Strategy
Passing a function as an argument entirely encapsulates the Strategy pattern in a functional paradigm.
Runic Tags:
#divination
#strategy
#gleam