The Decorator (Racket)
Dynamically attach new responsibilities to a function.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Decorator
ARCANUM SCHOOL: Enchantment // Weaving
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// RACKET PATTERN //
#lang racket
(define (base-attack)
"Strike!")
(define (with-fire base-func)
(λ ()
(string-append (base-func) " + Fire Damage")))
(define (with-poison base-func)
(λ ()
(string-append (base-func) " + Poison Damage")))
(define flaming-poison-attack (with-poison (with-fire base-attack)))
(displayln (flaming-poison-attack))
The Decorator
Instead of subclassing or rewriting, we wrap functions within functions. The Decorator pattern is inherently natural to Racket. By taking a base function and returning a new closure that augments its output, we can weave multiple enchantments onto a single strike with pure Parenthetical Evolution.
Runic Tags:
#racket
#structural
#decorator
#functional-magic