🚀 RACKET METAMAGIC DIFFICULTY: Adept Structural

The Bridge (Racket)

Decoupling an abstraction from its implementation so the two can vary independently.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Bridge
ARCANUM SCHOOL: Transmutation // Duality
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// RACKET PATTERN //
#lang racket

;; Implementations (The Elemental Sources)
(define (fire-source) "Flames")
(define (ice-source) "Frost")

;; Abstractions (The Spells)
(define (make-bolt source)
  (λ () (format "Shooting a bolt of ~a!" (source))))

(define (make-shield source)
  (λ () (format "Raising a shield of ~a!" (source))))

;; Bridging them
(define fire-bolt (make-bolt fire-source))
(define ice-shield (make-shield ice-source))

(displayln (fire-bolt))
(displayln (ice-shield))

The Bridge

The Bridge pattern ensures that spells and their elemental sources are not hard-coded together. By passing the elemental source as a function to the spell constructor, Racket allows us to seamlessly cross the bridge between abstract invocation and concrete manifestation. Macro Metamagic at its finest!

Runic Tags: #racket #structural #bridge #functional-magic