Skip to content

Commit e98ad67

Browse files
committed
iluwatar#590 add explanation for Iterator pattern
1 parent 4264f52 commit e98ad67

1 file changed

Lines changed: 99 additions & 0 deletions

File tree

iterator/README.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,105 @@ Cursor
1515
Provide a way to access the elements of an aggregate object
1616
sequentially without exposing its underlying representation.
1717

18+
## Explanation
19+
20+
Real world example
21+
22+
> Treasure chest contains a set of magical items. There multiple types of items such as rings, potions and weapons. The items can be browsed by type using an iterator the treasure chest provides.
23+
24+
In plain words
25+
26+
> Containers can provide a representation agnostic iterator interface to provide access to the elements.
27+
28+
Wikipedia says
29+
30+
> In object-oriented programming, the iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements.
31+
32+
**Programmatic Example**
33+
34+
The main class in our example is the treasure chest that contains items.
35+
36+
```java
37+
public class TreasureChest {
38+
39+
private List<Item> items;
40+
41+
public TreasureChest() {
42+
items = List.of(
43+
new Item(ItemType.POTION, "Potion of courage"),
44+
new Item(ItemType.RING, "Ring of shadows"),
45+
new Item(ItemType.POTION, "Potion of wisdom"),
46+
new Item(ItemType.POTION, "Potion of blood"),
47+
new Item(ItemType.WEAPON, "Sword of silver +1"),
48+
new Item(ItemType.POTION, "Potion of rust"),
49+
new Item(ItemType.POTION, "Potion of healing"),
50+
new Item(ItemType.RING, "Ring of armor"),
51+
new Item(ItemType.WEAPON, "Steel halberd"),
52+
new Item(ItemType.WEAPON, "Dagger of poison"));
53+
}
54+
55+
public Iterator<Item> iterator(ItemType itemType) {
56+
return new TreasureChestItemIterator(this, itemType);
57+
}
58+
59+
public List<Item> getItems() {
60+
return new ArrayList<>(items);
61+
}
62+
}
63+
64+
public class Item {
65+
66+
private ItemType type;
67+
private String name;
68+
69+
public Item(ItemType type, String name) {
70+
this.setType(type);
71+
this.name = name;
72+
}
73+
74+
@Override
75+
public String toString() {
76+
return name;
77+
}
78+
79+
public ItemType getType() {
80+
return type;
81+
}
82+
83+
public final void setType(ItemType type) {
84+
this.type = type;
85+
}
86+
}
87+
88+
public enum ItemType {
89+
90+
ANY, WEAPON, RING, POTION
91+
92+
}
93+
```
94+
95+
The iterator interface is extremely simple.
96+
97+
```java
98+
public interface Iterator<T> {
99+
100+
boolean hasNext();
101+
102+
T next();
103+
}
104+
```
105+
106+
In the following example we iterate through the ring type items found in the chest.
107+
108+
```java
109+
var itemIterator = TREASURE_CHEST.iterator(ItemType.RING);
110+
while (itemIterator.hasNext()) {
111+
LOGGER.info(itemIterator.next().toString());
112+
}
113+
// Ring of shadows
114+
// Ring of armor
115+
```
116+
18117
## Class diagram
19118
![alt text](./etc/iterator_1.png "Iterator")
20119

0 commit comments

Comments
 (0)