🚀 RACKET METAMAGIC DIFFICULTY: Apprentice Behavioral

The State (Racket)

Altering an entity's behavior when its internal state changes.

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

(define (make-elemental)
  (let ([state 'water])
    (define (freeze!) (set! state 'ice))
    (define (boil!) (set! state 'steam))
    (define (attack)
      (case state
        [(water) "Splashes gently."]
        [(ice) "Pierces with icicles!"]
        [(steam) "Scalds with hot vapor!"]))
    (hash 'freeze! freeze!
          'boil! boil!
          'attack attack)))

(define my-elemental (make-elemental))
(displayln ((hash-ref my-elemental 'attack)))

((hash-ref my-elemental 'freeze!))
(displayln ((hash-ref my-elemental 'attack)))

The State

An elemental spirit behaves differently depending on its current form. The State pattern encapsulates state-specific behaviors inside a single object. By mutating the internal state variable, the elemental dramatically shifts its attack patterns without changing its outward identity.

Runic Tags: #racket #behavioral #state #shapeshifting