🐺 WOLFRAM ALCHEMY DIFFICULTY: Archmage Behavioral

The Memento Incantation

Capturing and restoring the internal state of a ritual without violating encapsulation.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Memento
ARCANUM SCHOOL: Chronomancy // Restoration
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// MATHEMATICA PATTERN //
(* The Originator *)
ClearAll[CreateRitual, SetRitualState, SaveToMemento, RestoreFromMemento];

CreateRitual[] := Module[{state = "Idle"},
  <|
    "SetState" -> Function[newState, state = newState; Print["Ritual state set to: ", state]],
    "Save" -> Function[], state],
    "Restore" -> Function[memento, state = memento; Print["Ritual state restored to: ", state]]
  |>
];

(* Due to how closures capture variables, we rewrite it properly using a closure state variable *)
RitualObject[] := Module[{internalState = "Idle"},
  Function[cmd,
    Switch[cmd,
      "Get", internalState,
      "Set", Function[val, internalState = val; val],
      "Save", internalState,
      "Restore", Function[memento, internalState = memento; memento]
    ]
  ]
];

(* Usage *)
ritual = RitualObject[];
ritual["Set"]["Chanting"];

(* Save point *)
memento1 = ritual["Save"];

ritual["Set"]["Sacrifice"];

(* Oh no, it went wrong, rewind! *)
ritual["Restore"][memento1];
Print["Current State: ", ritual["Get"]];
Runic Tags: #memento #behavioral #chronomancy #state-saving