You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: iterator/README.md
+28-12Lines changed: 28 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,29 +9,34 @@ tags:
9
9
---
10
10
11
11
## Also known as
12
+
12
13
Cursor
13
14
14
15
## Intent
15
-
Provide a way to access the elements of an aggregate object
16
-
sequentially without exposing its underlying representation.
16
+
Provide a way to access the elements of an aggregate object sequentially without exposing its
17
+
underlying representation.
17
18
18
19
## Explanation
19
20
20
21
Real world example
21
22
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
+
> Treasure chest contains a set of magical items. There multiple types of items such as rings,
24
+
> potions and weapons. The items can be browsed by type using an iterator the treasure chest
25
+
> provides.
23
26
24
27
In plain words
25
28
26
-
> Containers can provide a representation agnostic iterator interface to provide access to the elements.
29
+
> Containers can provide a representation agnostic iterator interface to provide access to the
30
+
> elements.
27
31
28
32
Wikipedia says
29
33
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.
34
+
> In object-oriented programming, the iterator pattern is a design pattern in which an iterator is
35
+
> used to traverse a container and access the container's elements.
31
36
32
37
**Programmatic Example**
33
38
34
-
The main class in our example is the treasure chest that contains items.
39
+
The main class in our example is the `TreasureChest` that contains items.
35
40
36
41
```java
37
42
publicclassTreasureChest {
@@ -60,7 +65,11 @@ public class TreasureChest {
60
65
returnnewArrayList<>(items);
61
66
}
62
67
}
68
+
```
69
+
70
+
Here's the `Item` class:
63
71
72
+
```java
64
73
publicclassItem {
65
74
66
75
privateItemType type;
@@ -92,7 +101,7 @@ public enum ItemType {
92
101
}
93
102
```
94
103
95
-
The iterator interface is extremely simple.
104
+
The `Iterator` interface is extremely simple.
96
105
97
106
```java
98
107
publicinterfaceIterator<T> {
@@ -110,19 +119,26 @@ var itemIterator = TREASURE_CHEST.iterator(ItemType.RING);
110
119
while (itemIterator.hasNext()) {
111
120
LOGGER.info(itemIterator.next().toString());
112
121
}
113
-
// Ring of shadows
114
-
// Ring of armor
122
+
```
123
+
124
+
Program output:
125
+
126
+
```java
127
+
Ring of shadows
128
+
Ring of armor
115
129
```
116
130
117
131
## Class diagram
132
+
118
133

119
134
120
135
## Applicability
136
+
121
137
Use the Iterator pattern
122
138
123
-
*to access an aggregate object's contents without exposing its internal representation
124
-
*to support multiple traversals of aggregate objects
125
-
*to provide a uniform interface for traversing different aggregate structures
139
+
*To access an aggregate object's contents without exposing its internal representation.
140
+
*To support multiple traversals of aggregate objects.
141
+
*To provide a uniform interface for traversing different aggregate structures.
0 commit comments