The Iterator
Sequentially accessing the elements of an arcane matrix without exposing its underlying structure.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Iterator
ARCANUM SCHOOL: Divination // Scrying
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// CODEX INCANTATION //
interface RunicIterator<T> {
next(): T | null;
hasNext(): boolean;
}
class SpellBookIterator implements RunicIterator<string> {
private position = 0;
constructor(private spells: string[]) {}
hasNext(): boolean {
return this.position < this.spells.length;
}
next(): string | null {
if (this.hasNext()) {
return this.spells[this.position++];
}
return null;
}
}
The Iterator pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation. Whether browsing a spellbook’s pages or scanning nodes in a cyber-net, it offers a standardized, typed mechanism to traverse the unknown.
Runic Tags:
#behavioral
#typescript
#iterator