⚔️ RPG WARD DIFFICULTY: Apprentice Creational

Singleton: The Core DB2 Connection

Ensure a Ward has only one instance, and provide a global point of access to it.

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

Dcl-S SingleInstance Pointer Static Inz(*Null);

Dcl-Ds Config_t Qualified Template;
  DbName Char(20);
  IsActive Ind;
End-Ds;

Dcl-Proc GetConfigInstance Export;
  Dcl-Pi *N Pointer;
  End-Pi;

  Dcl-Ds Config Likeds(Config_t) Based(SingleInstance);

  If SingleInstance = *Null;
     SingleInstance = %Alloc(%Size(Config_t));
     Config.DbName = 'PRODDB';
     Config.IsActive = *On;
  EndIf;

  Return SingleInstance;
End-Proc;

Singleton

The Singleton is an essential ward for caching DB2 connections or system configuration properties in RPGLE. Using a static variable scoped to the module ensures that the runtime only ever spawns a single copy of this critical structure.

Runic Tags: #creational #ibm-i #runes #singleton