From ed385fdd2e4dece2eeec57337b8276b1f23be5ac Mon Sep 17 00:00:00 2001 From: PhucVR <59957741+nguyenphuc22@users.noreply.github.com> Date: Sun, 31 Dec 2023 20:28:15 +0700 Subject: [PATCH] Update Iterator --- .idea/workspace.xml | 8 ++++---- Writerside/.DS_Store | Bin 8196 -> 8196 bytes Writerside/topics/Iterator.md | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 4 deletions(-) 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 6a1cd21b51a2c111c7f0427db9e99bfafab85de8..9242625866d7ae66d3356a59bbe9f481f897f035 100644 GIT binary patch delta 88 zcmZp1XmOa}&&|fbz`)4BAi%(ox-n`aKR<}i#*oR7%aF*B&X77;O5lt*oX1dH7F?8< hlb@Fk(zTgKfQx-IyTmsZCMNsI4+VrbD~fz#0sugJ6siCK delta 45 zcmZp1XmOa}&&|TXz`)4BAi%(oyfJDcKO@WLzXDwB6B|BmW|#QJviY5;B-6x(cK{3< B4k`cu 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.