diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 45d6bc8..e33fd34 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -65,18 +65,18 @@ "RunOnceActivity.OpenProjectViewOnStart": "true", "RunOnceActivity.ShowReadmeOnStart": "true", "git-widget-placeholder": "main", - "last_opened_file_path": "/Users/phucnguyen/Documents/GitHub/Design-Patterns/Writerside/topics" + "last_opened_file_path": "/Users/phucnguyen/Documents/GitHub/Design-Patterns/Writerside/topics", + "settings.editor.selected.configurable": "fileTemplates" }, "keyToStringList": { "stardust.markdown.MarkdownSplitEditorSuppressor:keyList": [ - "/Users/phucnguyen/Documents/GitHub/Design-Patterns/Writerside/topics/Behavioral-Patterns.md", "/Users/phucnguyen/Documents/GitHub/Design-Patterns/Writerside/topics/Builder.md", "/Users/phucnguyen/Documents/GitHub/Design-Patterns/Writerside/topics/Chain-Of-Responsibility.md", "/Users/phucnguyen/Documents/GitHub/Design-Patterns/Writerside/topics/Command.md", + "/Users/phucnguyen/Documents/GitHub/Design-Patterns/Writerside/topics/Creational-Patterns.md", "/Users/phucnguyen/Documents/GitHub/Design-Patterns/Writerside/topics/Intro.md", "/Users/phucnguyen/Documents/GitHub/Design-Patterns/Writerside/topics/Proxy.md", - "/Users/phucnguyen/Documents/GitHub/Design-Patterns/Writerside/topics/Strategy.md", - "/Users/phucnguyen/Documents/GitHub/Design-Patterns/Writerside/topics/Template-Method.md", + "/Users/phucnguyen/Documents/GitHub/Design-Patterns/Writerside/topics/Singleton.md", "/Users/phucnguyen/Documents/GitHub/Design-Patterns/Writerside/topics/Visitor.md" ] } diff --git a/Writerside/.DS_Store b/Writerside/.DS_Store index 6a1cd21..9242625 100644 Binary files a/Writerside/.DS_Store and b/Writerside/.DS_Store differ diff --git a/Writerside/topics/Iterator.md b/Writerside/topics/Iterator.md index 9e5ff06..87b156b 100644 --- a/Writerside/topics/Iterator.md +++ b/Writerside/topics/Iterator.md @@ -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 { + <> + +next() item + +hasNext() bool + +currentItem() item + } + + class Collection { + <> + +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.