⚔️ RPG WARD DIFFICULTY: Adept Structural

Flyweight: The Shared Essence

Use sharing to support large numbers of fine-grained Runes efficiently.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Flyweight
ARCANUM SCHOOL: Transmutation // Material
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// RPG PATTERN //
**FREE
Ctl-Opt NoMain;

Dcl-Ds IntrinsicState_t Qualified Template;
  Name Char(10);
  Type Char(10);
End-Ds;

Dcl-Ds Cache Dim(100) Likeds(IntrinsicState_t) Static;
Dcl-S CacheCount Int(10) Static Inz(0);

Dcl-Proc GetFlyweight Export;
  Dcl-Pi *N Pointer;
    RequestedName Char(10) Const;
  End-Pi;

  Dcl-S i Int(10);
  For i = 1 to CacheCount;
     If Cache(i).Name = RequestedName;
        Return %Addr(Cache(i));
     EndIf;
  EndFor;

  CacheCount += 1;
  Cache(CacheCount).Name = RequestedName;
  Cache(CacheCount).Type = 'RUNE';
  Return %Addr(Cache(CacheCount));
End-Proc;

Flyweight

Memory on the AS/400 is vast, but allocating thousands of identical Wards is wasteful. The Flyweight caches intrinsic configurations so that thousands of Extrinsic invocations can point to the same physical Data Structure.

Runic Tags: #structural #ibm-i #runes #flyweight