🐺 WOLFRAM ALCHEMY DIFFICULTY: Adept Behavioral

The Command Incantation

Encapsulating a spell cast as an object to allow for deferred execution or undoing.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Command
ARCANUM SCHOOL: Enchantment // Binding
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// MATHEMATICA PATTERN //
(* The Command Object *)
ClearAll[CreateSpellCommand, ExecuteCommand, UndoCommand];

CreateSpellCommand[spell_, target_] := 
  <|"Spell" -> spell, "Target" -> target, "Status" -> "Prepared"|>;

(* The Invoker *)
ExecuteCommand[cmd_Association] := Module[{updatedCmd},
  Print["Casting ", cmd["Spell"], " on ", cmd["Target"]];
  updatedCmd = Append[cmd, "Status" -> "Cast"];
  updatedCmd
];

UndoCommand[cmd_Association] := Module[{updatedCmd},
  If[cmd["Status"] === "Cast",
    Print["Reversing ", cmd["Spell"], " on ", cmd["Target"]];
    updatedCmd = Append[cmd, "Status" -> "Reversed"];
    updatedCmd,

    Print["Cannot reverse an uncast spell."];
    cmd
  ]
];

(* Usage *)
cmd = CreateSpellCommand["Petrify", "Goblin"];
executed = ExecuteCommand[cmd];
reversed = UndoCommand[executed];
Runic Tags: #command #behavioral #deferred #associations