The Chain of Responsibility (Racket)
Passing a mystical request along a chain of handlers.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Chain of Responsibility
ARCANUM SCHOOL: Enchantment // Threads
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// RACKET PATTERN //
#lang racket
(define (make-handler condition action next-handler)
(λ (request)
(if (condition request)
(action request)
(if next-handler
(next-handler request)
(printf "Request ~a unhandled.\n" request)))))
(define fire-handler
(make-handler (λ (r) (eq? r 'fire))
(λ (r) (displayln "Handled by Fire Mage"))
#f))
(define ice-handler
(make-handler (λ (r) (eq? r 'ice))
(λ (r) (displayln "Handled by Ice Mage"))
fire-handler))
(define arcane-handler
(make-handler (λ (r) (eq? r 'arcane))
(λ (r) (displayln "Handled by Arcane Mage"))
ice-handler))
(arcane-handler 'ice)
(arcane-handler 'fire)
(arcane-handler 'shadow)
The Chain of Responsibility
By linking closures together, we form an unbroken thread of execution. When an arcane anomaly is detected, it is passed down the chain. Each Mage in the circle inspects the request; if their elements align, they handle it, otherwise, it flows to the next. Metamagic gracefully untangled.
Runic Tags:
#racket
#behavioral
#chain-of-responsibility
#functional-magic