Skip to content

Commit 5e12bc5

Browse files
committed
docs: improve iterator
1 parent 58ec27d commit 5e12bc5

File tree

4 files changed

+52
-44
lines changed

4 files changed

+52
-44
lines changed

iterator/README.md

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,35 @@ title: Iterator
33
category: Behavioral
44
language: en
55
tag:
6-
- Gang of Four
6+
- Data access
7+
- Data transformation
8+
- Decoupling
9+
- Gang of Four
10+
- Object composition
11+
- Polymorphism
712
---
813

914
## Also known as
1015

11-
Cursor
16+
* Cursor
1217

1318
## 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.
1621

1722
## Explanation
1823

1924
Real-world example
2025

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.
2427
2528
In plain words
2629

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.
2931
3032
Wikipedia says
3133

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.
3435
3536
**Programmatic Example**
3637

@@ -128,7 +129,7 @@ Ring of armor
128129

129130
## Class diagram
130131

131-
![alt text](./etc/iterator_1.png "Iterator")
132+
![Iterator](./etc/iterator_1.png "Iterator")
132133

133134
## Applicability
134135

@@ -144,10 +145,30 @@ Use the Iterator pattern
144145

145146
## Known uses
146147

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.
147150
* [java.util.Iterator](http://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html)
148151
* [java.util.Enumeration](http://docs.oracle.com/javase/8/docs/api/java/util/Enumeration.html)
149152

153+
## Consequences
154+
155+
Benefits:
156+
157+
* 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+
150171
## Credits
151172

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)

iterator/src/main/java/com/iluwatar/iterator/bst/TreeNode.java

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
*/
2525
package com.iluwatar.iterator.bst;
2626

27+
import lombok.Getter;
28+
import lombok.Setter;
29+
2730
/**
2831
* TreeNode Class, representing one node in a Binary Search Tree. Allows for a generically typed
2932
* value.
@@ -33,7 +36,13 @@
3336
public class TreeNode<T extends Comparable<T>> {
3437

3538
private final T val;
39+
40+
@Getter
41+
@Setter
3642
private TreeNode<T> left;
43+
44+
@Getter
45+
@Setter
3746
private TreeNode<T> right;
3847

3948
/**
@@ -51,22 +60,6 @@ public T getVal() {
5160
return val;
5261
}
5362

54-
public TreeNode<T> getLeft() {
55-
return left;
56-
}
57-
58-
private void setLeft(TreeNode<T> left) {
59-
this.left = left;
60-
}
61-
62-
public TreeNode<T> getRight() {
63-
return right;
64-
}
65-
66-
private void setRight(TreeNode<T> right) {
67-
this.right = right;
68-
}
69-
7063
/**
7164
* Inserts new TreeNode based on a given value into the subtree represented by self.
7265
*

iterator/src/main/java/com/iluwatar/iterator/list/Item.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,23 @@
2424
*/
2525
package com.iluwatar.iterator.list;
2626

27+
import lombok.AllArgsConstructor;
28+
import lombok.Getter;
29+
import lombok.Setter;
30+
2731
/**
2832
* Item.
2933
*/
34+
@AllArgsConstructor
3035
public class Item {
3136

37+
@Getter
38+
@Setter
3239
private ItemType type;
3340
private final String name;
3441

35-
public Item(ItemType type, String name) {
36-
this.setType(type);
37-
this.name = name;
38-
}
39-
4042
@Override
4143
public String toString() {
4244
return name;
4345
}
44-
45-
public ItemType getType() {
46-
return type;
47-
}
48-
49-
public final void setType(ItemType type) {
50-
this.type = type;
51-
}
5246
}

iterator/src/test/java/com/iluwatar/iterator/list/TreasureChestTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ void testIterator(Item expectedItem) {
9292
*/
9393
@ParameterizedTest
9494
@MethodSource("dataProvider")
95-
void testGetItems(Item expectedItem) throws Exception {
95+
void testGetItems(Item expectedItem) {
9696
final var chest = new TreasureChest();
9797
final var items = chest.getItems();
9898
assertNotNull(items);

0 commit comments

Comments
 (0)