Back to Home
Java
Design Patterns
Iterator
Harry Potter

Design Patterns: Iterator

The Marauder's Map

14.12.2023
5 min read
Dustin Maurer
Design Patterns: Iterator

Design Patterns: Iterator

The Marauder's Map

Overview:

The Iterator design pattern is akin to the Marauder's Map, a magical parchment that unveils the layout of Hogwarts, enabling its user to navigate through various locations discreetly. Similarly, the Iterator pattern allows traversal through elements of a collection without exposing its internal structure.

Explanation:

Consider a scenario where Hogwarts has various secret passages, rooms, and corridors. The Marauder's Map serves as a guide, allowing individuals to move through these locations without revealing the castle's complete layout.

Likewise, in software development, an Iterator serves as a guide through a collection of elements. Imagine a spellbook containing various enchantments. Using an Iterator, a wizard can move through the spellbook's contents (elements) sequentially without knowing how they are organized internally.

Pseudo-code Example:


// Let's create a spellbook class that implements the Iterator pattern

// Iterator interface

interface SpellIterator {

boolean hasNext();

Spell next();

}

// Concrete Iterator

class SpellbookIterator implements SpellIterator {

private Spell[] spells;

private int position = 0;

public SpellbookIterator(Spell[] spells) {

this.spells = spells;

}

@Override

public boolean hasNext() {

return position < spells.length;

}

@Override

public Spell next() {

if (hasNext()) {

return spells[position++];

}

return null;

}

}

// Spellbook class implementing Iterable

class Spellbook implements Iterable {

private Spell[] spells;

public Spellbook(Spell[] spells) {

this.spells = spells;

}

@Override

public SpellIterator iterator() {

return new SpellbookIterator(spells);

}

}

// Usage

Spell[] spells = {new Spell("Lumos"), new Spell("Alohomora"), new Spell("Expelliarmus")};

Spellbook spellbook = new Spellbook(spells);

for (Spell spell : spellbook) {

System.out.println(spell.getName()); // Accessing spells sequentially

}

In this example, the Spellbook class encapsulates a collection of spells and provides an Iterator (SpellbookIterator) to traverse through the spells sequentially, much like using the Marauder's Map to navigate Hogwarts discreetly.

Summary:

The Iterator pattern abstracts the process of accessing elements in a collection, shielding the user from the internal structure. It provides a standardized way to traverse through a collection without exposing its implementation details, similar to how the Marauder's Map guides individuals through Hogwarts without revealing its entire layout.

Another Example - how to use the Iterator Pattern:

Iterator for Magical Creatures

Let's define a MagicalCreature class and a CreatureCollection class that will use an Iterator to traverse through these creatures:


// Magical Creature class

class MagicalCreature {

private String name;

public MagicalCreature(String name) {

this.name = name;

}

public String getName() {

return name;

}

}

Creature Collection and Iterator:


import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

// Iterator interface

interface CreatureIterator {

boolean hasNext();

MagicalCreature next();

}

// Concrete Iterator

class CreatureCollectionIterator implements CreatureIterator {

private List creatures;

private int position = 0;

public CreatureCollectionIterator(List creatures) {

this.creatures = creatures;

}

@Override

public boolean hasNext() {

return position < creatures.size();

}

@Override

public MagicalCreature next() {

if (hasNext()) {

return creatures.get(position++);

}

return null;

}

}

// Creature Collection class implementing Iterable

class CreatureCollection implements Iterable {

private List creatures;

public CreatureCollection() {

this.creatures = new ArrayList<>();

}

public void addCreature(MagicalCreature creature) {

creatures.add(creature);

}

@Override

public Iterator iterator() {

return new CreatureCollectionIterator(creatures);

}

}

// Usage

public class Main {

public static void main(String[] args) {

CreatureCollection creatureCollection = new CreatureCollection();

creatureCollection.addCreature(new MagicalCreature("Hippogriff"));

creatureCollection.addCreature(new MagicalCreature("Blast-Ended Skrewt"));

creatureCollection.addCreature(new MagicalCreature("Thestral"));

for (MagicalCreature creature : creatureCollection) {

System.out.println("Encountered: " + creature.getName());

}

}

}

Here, the CreatureCollection class manages a collection of MagicalCreature objects. It provides an Iterator (CreatureCollectionIterator) that allows iterating through these creatures sequentially.

The Iterator pattern abstracts the traversal process, allowing us to iterate through the collection (CreatureCollection) without needing to know its internal structure. This enables easy and uniform access to the magical creatures, much like an explorer navigating through Hagrid's hut and encountering different magical creatures without knowing how they are arranged within the hut's space.

This demonstrates how the Iterator pattern simplifies the traversal of collections, providing a standardized way to access elements without revealing the collection's implementation details.

Weitere Artikel