The Blind Iterator
Traversing the labyrinthine collections of the Cathedral without uncovering their underlying structure.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Iterator
ARCANUM SCHOOL: Divination // Traversal
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// JAVA PATTERN //
import java.util.NoSuchElementException;
public interface ReliquaryIterator {
boolean hasNext();
String next();
}
public class ArrayReliquary {
private final String[] relics;
public ArrayReliquary(String[] relics) {
this.relics = relics;
}
public ReliquaryIterator createIterator() {
return new ReliquaryIteratorImpl();
}
private class ReliquaryIteratorImpl implements ReliquaryIterator {
private int index = 0;
@Override
public boolean hasNext() {
return index < relics.length;
}
@Override
public String next() {
if (!hasNext()) {
throw new NoSuchElementException("The reliquary is empty.");
}
return relics[index++];
}
}
}
The vaults of the Java Cathedral hold collections of unimaginable complexity. Exposing the bare arrays, trees, or graphs that house these artifacts is an architectural sin. The Iterator pattern binds the traversal logic into a separate, standardized construct.
By calling createIterator(), an adept is given a blind guide—a ReliquaryIterator. The caller simply invokes hasNext() and next(), completely oblivious to whether the underlying data is stored in a contiguous block of memory or scattered across a distributed cluster. It is the ultimate decoupling of traversal from structure.
Runic Tags:
#iterator
#traversal
#collections
#enterprise