The Strategy (Racket)
Encapsulating a family of algorithms and making them interchangeable.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Strategy
ARCANUM SCHOOL: Evocation // Tactics
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// RACKET PATTERN //
#lang racket
;; Strategies
(define (aggressive-stance enemy) (format "Charging head-on at ~a!" enemy))
(define (defensive-stance enemy) (format "Raising shields against ~a." enemy))
(define (stealth-stance enemy) (format "Sneaking around ~a." enemy))
;; Context
(define (engage enemy strategy)
(displayln (strategy enemy)))
(engage "Orc Warlord" aggressive-stance)
(engage "Dragon" defensive-stance)
The Strategy
In languages where functions are first-class citizens, the Strategy pattern is effortlessly native. Instead of building complex class hierarchies, we simply pass the tactical algorithm (the function) into the execution context. Parenthetical Evolution at its sleekest!
Runic Tags:
#racket
#behavioral
#strategy
#functional-magic