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
+35-14Lines changed: 35 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -3,34 +3,35 @@ title: Iterator
3
3
category: Behavioral
4
4
language: en
5
5
tag:
6
-
- Gang of Four
6
+
- Data access
7
+
- Data transformation
8
+
- Decoupling
9
+
- Gang of Four
10
+
- Object composition
11
+
- Polymorphism
7
12
---
8
13
9
14
## Also known as
10
15
11
-
Cursor
16
+
*Cursor
12
17
13
18
## Intent
14
-
Provide a way to access the elements of an aggregate object sequentially without exposing its
15
-
underlying representation.
19
+
20
+
The Iterator pattern provides a way to access elements of an aggregate object sequentially without exposing its underlying representation.
16
21
17
22
## Explanation
18
23
19
24
Real-world example
20
25
21
-
> Treasure chest contains a set of magical items. There multiple types of items such as rings,
22
-
> potions, and weapons. The items can be browsed by type using an iterator the treasure chest
23
-
> provides.
26
+
> Imagine visiting a library with a vast collection of books organized in different sections such as fiction, non-fiction, science, etc. Instead of searching through every shelf yourself, the librarian provides you with a specific guidebook or a digital catalog for each section. This guidebook acts as an "iterator," allowing you to go through the books section by section, or even skip to specific types of books, without needing to know how the books are organized on the shelves. Each guidebook handles the traversal through its section, providing a consistent and efficient way to access the books, much like how the Iterator design pattern offers a uniform method to traverse different data structures in a software application.
24
27
25
28
In plain words
26
29
27
-
> Containers can provide a representation agnostic iterator interface to provide access to the
28
-
> elements.
30
+
> The Iterator design pattern provides a way to access the elements of a collection sequentially without exposing the underlying structure of the collection.
29
31
30
32
Wikipedia says
31
33
32
-
> In object-oriented programming, the iterator pattern is a design pattern in which an iterator is
33
-
> 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 used to traverse a container and access the container's elements.
34
35
35
36
**Programmatic Example**
36
37
@@ -128,7 +129,7 @@ Ring of armor
128
129
129
130
## Class diagram
130
131
131
-

132
+

132
133
133
134
## Applicability
134
135
@@ -144,10 +145,30 @@ Use the Iterator pattern
144
145
145
146
## Known uses
146
147
148
+
* Java Collections Framework utilizes iterators extensively to allow different ways to traverse through collections.
149
+
* Databases often use iterators to navigate through data records fetched through SQL queries.
* Reduces the coupling between data structures and algorithms used for iteration.
158
+
* Provides a uniform interface for iterating over various types of data structures, enhancing code reusability and flexibility.
159
+
160
+
Trade-offs:
161
+
162
+
* Overhead of using an iterator object may slightly reduce performance compared to direct traversal methods.
163
+
* Complex aggregate structures may require complex iterators that can be difficult to manage or extend.
164
+
165
+
## Related Patterns
166
+
167
+
*[Composite](https://java-design-patterns.com/patterns/composite/): Iterators are often used to traverse Composite trees.
168
+
*[Factory Method](https://java-design-patterns.com/patterns/factory-method/): Used to create appropriate iterators for different data structures.
169
+
*[Visitor](https://java-design-patterns.com/patterns/visitor/): Can be used with Iterator to apply operations over elements of an object structure.
170
+
150
171
## Credits
151
172
152
-
*[Design Patterns: Elements of Reusable Object-Oriented Software](https://www.amazon.com/gp/product/0201633612/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0201633612&linkCode=as2&tag=javadesignpat-20&linkId=675d49790ce11db99d90bde47f1aeb59)
153
-
*[Head First Design Patterns: A Brain-Friendly Guide](https://www.amazon.com/gp/product/0596007124/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596007124&linkCode=as2&tag=javadesignpat-20&linkId=6b8b6eea86021af6c8e3cd3fc382cb5b)
173
+
*[Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI)
174
+
*[Head First Design Patterns: Building Extensible and Maintainable Object-Oriented Software](https://amzn.to/49NGldq)
0 commit comments