🐺 WOLFRAM ALCHEMY DIFFICULTY: Adept Structural

The Flyweight Incantation

Sharing common symbolic states to minimize the kernel's memory footprint when summoning vast swarms.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Flyweight
ARCANUM SCHOOL: Conjuration // Efficiency
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// MATHEMATICA PATTERN //
(* The Shared State Cache (Flyweight Factory) *)
Begin["`Private`"];
ClearAll[GetRuneArchetype, $RuneCache];

$RuneCache = <||>;

GetRuneArchetype[type_] := 
  If[KeyExistsQ[$RuneCache, type],
    $RuneCache[type],

    Print["Forging new archetype for: ", type];
    $RuneCache[type] = <|"Type" -> type, "Cost" -> StringLength[type]|>;
    $RuneCache[type]
  ];
End[];

(* The Extrinsic State Manifestation *)
CastSwarm[type_, count_] := Module[{archetype},
  archetype = GetRuneArchetype[type];
  Table[Join[archetype, <|"ID" -> i, "Position" -> RandomReal[{0, 10}, 2]|>], {i, count}]
];

(* Usage *)
swarm1 = CastSwarm["Locust", 3];
swarm2 = CastSwarm["Locust", 2];
Runic Tags: #flyweight #structural #memory #caching