Skip to content

Commit

Permalink
Update Iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenphuc22 committed Dec 31, 2023
1 parent e745e64 commit ed385fd
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
8 changes: 4 additions & 4 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified Writerside/.DS_Store
Binary file not shown.
32 changes: 32 additions & 0 deletions Writerside/topics/Iterator.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,38 @@ Có vẻ đơn giản nếu `collection` là dạng list. Việc duyệt qua cá

## Giải pháp

```mermaid
classDiagram
class Iterator {
<<interface>>
+next() item
+hasNext() bool
+currentItem() item
}
class Collection {
<<interface>>
+createIterator() Iterator
}
class ConcreteCollection {
-items[]
+createIterator() Iterator
}
class ConcreteIterator {
-collection
-currentIndex
+next() item
+hasNext() bool
+currentItem() item
}
Collection <|-- ConcreteCollection
Iterator <|-- ConcreteIterator
ConcreteIterator "1" -- "*" ConcreteCollection: navigates >
```

Giải pháp cho thách thức này chính là áp dụng mẫu thiết kế `Iterator`. Mẫu thiết kế này giúp tách biệt hoàn toàn hành vi duyệt qua các phần tử của `collection` ra khỏi cấu trúc của `collection` đó. Điều này được thực hiện thông qua việc tạo ra một đối tượng riêng biệt, gọi là `iterator`.

Một `iterator` không chỉ thực thi thuật toán duyệt qua các phần tử, mà còn đóng gói chi tiết về quá trình duyệt. Điều này bao gồm việc theo dõi vị trí hiện tại trong `collection` và xác định xem còn bao nhiêu phần tử nữa trước khi hoàn thành quá trình duyệt.
Expand Down

0 comments on commit ed385fd

Please sign in to comment.