🟢 CLOJURE GLYPHS DIFFICULTY: Adept Behavioral

The State Incantation

Altering an entity's behavior as its internal energy matrix changes.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: State
ARCANUM SCHOOL: Transmutation // Shifting
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// CLOJURE PATTERN //
(ns tekromancy.state)

(defmulti attack :stance)

(defmethod attack :offensive [_]
  "Swings wildly with reckless abandon!")

(defmethod attack :defensive [_]
  "Strikes carefully from behind a shield.")

(defmethod attack :berserk [_]
  "Roars and cleaves everything in sight!")

(defn change-stance [entity new-stance]
  (assoc entity :stance new-stance))

;; Usage:
;; (def warrior {:stance :defensive})
;; (attack warrior)
;; (attack (change-stance warrior :berserk))

As a spellcaster shifts stances, their very nature changes. Polymorphism handles this flawlessly. Using multimethods dispatched on a state key within a map, an entity behaves dynamically based on its internal state, encapsulating the transitions without massive conditional chains.

Runic Tags: #behavioral #state #clojure #multimethods