The Flyweight Hex
Conserving magical memory by sharing intrinsic arcane state among vast swarms of entities.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Flyweight
ARCANUM SCHOOL: Illusion // Actor Model Hexes
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// MOTOKO PATTERN //
module Flyweight {
// Intrinsic, shared state
public type FamiliarEssence = {
species : Text;
color : Text;
aura : Text;
};
public class EssenceRegistry() {
var batEssence : ?FamiliarEssence = null;
public func getBatEssence() : FamiliarEssence {
switch (batEssence) {
case (null) {
let e = { species = "Bat"; color = "Black"; aura = "Shadow" };
batEssence := ?e;
e;
};
case (?e) { e };
};
};
};
// Extrinsic, unique state
public type SwarmMember = {
x : Nat;
y : Nat;
essence : FamiliarEssence;
};
public actor VampireLord {
let registry = EssenceRegistry();
var swarm : [SwarmMember] = [];
public func spawnBat(x: Nat, y: Nat) : async () {
let e = registry.getBatEssence();
let bat : SwarmMember = { x = x; y = y; essence = e };
swarm := Array.append(swarm, [bat]);
};
};
}
When calling forth swarms of bats, replicating their identical traits wastes precious mental weave. The Flyweight Hex extracts the intrinsic FamiliarEssence into a shared registry, allowing thousands of actors to merely reference the essence while managing their own unique coordinates.
Runic Tags:
#motoko
#structural
#flyweight
#actor-model