From a86a3deb5791d7bc1ad3e5f644bbfbaaae83fe36 Mon Sep 17 00:00:00 2001 From: lantry-glitch <allan.kwek.18@gmail.com> Date: Fri, 28 Mar 2025 11:53:43 +0700 Subject: [PATCH] Add Reflection Publisher-1 --- README.md | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 3043f06..4c7d71c 100644 --- a/README.md +++ b/README.md @@ -48,15 +48,15 @@ You can install Postman via this website: https://www.postman.com/downloads/ (You might want to use `cargo check` if you only need to verify your work without running the app.) ## Mandatory Checklists (Publisher) -- [ ] Clone https://gitlab.com/ichlaffterlalu/bambangshop to a new repository. +- [✓] Clone https://gitlab.com/ichlaffterlalu/bambangshop to a new repository. - **STAGE 1: Implement models and repositories** - - [ ] Commit: `Create Subscriber model struct.` - - [ ] Commit: `Create Notification model struct.` - - [ ] Commit: `Create Subscriber database and Subscriber repository struct skeleton.` - - [ ] Commit: `Implement add function in Subscriber repository.` - - [ ] Commit: `Implement list_all function in Subscriber repository.` - - [ ] Commit: `Implement delete function in Subscriber repository.` - - [ ] Write answers of your learning module's "Reflection Publisher-1" questions in this README. + - [✓] Commit: `Create Subscriber model struct.` + - [✓] Commit: `Create Notification model struct.` + - [✓] Commit: `Create Subscriber database and Subscriber repository struct skeleton.` + - [✓] Commit: `Implement add function in Subscriber repository.` + - [✓] Commit: `Implement list_all function in Subscriber repository.` + - [✓] Commit: `Implement delete function in Subscriber repository.` + - [✓] Write answers of your learning module's "Reflection Publisher-1" questions in this README. - **STAGE 2: Implement services and controllers** - [ ] Commit: `Create Notification service struct skeleton.` - [ ] Commit: `Implement subscribe function in Notification service.` @@ -77,6 +77,26 @@ This is the place for you to write reflections: ### Mandatory (Publisher) Reflections #### Reflection Publisher-1 +1. In the Observer pattern diagram explained by the Head First Design Pattern book, Subscriber is defined as an interface. Explain based on your understanding of Observer design patterns, do we still need an interface (or trait in Rust) in this BambangShop case, or a single Model struct is enough? +<br> + +Berdasarkan pemahaman terhadap Observer pattern pada BambangShop saat ini, penggunaan single Model struct sudah cukup. Hal ini karena: + +- Saat ini hanya ada satu tipe subscriber (Observer), sehingga belum ada kebutuhan untuk abstraksi atau polimorfisme melalui trait atau interface. + +- Jika di masa depan muncul tipe-tipe subscriber baru dengan perilaku yang berbeda, penggunaan trait atau interface akan diperlukan untuk memisahkan perilaku observer dari implementasinya dan untuk menerapkan prinsip Open/Closed, di mana publisher tidak perlu diubah ketika menambah jenis subscriber baru. + +Dengan demikian, untuk saat ini solusi yang sederhana sudah memadai, dan kita bisa mempertimbangkan refactoring ke penggunaan trait apabila kompleksitas sistem bertambah. Pendekatan ini sesuai dengan prinsip desain SOLID, khususnya Open/Closed Principle, sehingga sistem dapat berkembang tanpa mengganggu komponen yang telah ada. +<br><br> + +2. id in Program and url in Subscriber is intended to be unique. Explain based on your understanding, is using Vec (list) sufficient or using DashMap (map/dictionary) like we currently use is necessary for this case? + +Mempertimbangkan keunggulan dalam pencarian dan manajemen data, DashMap menawarkan solusi yang lebih optimal. Pertama, DashMap menyediakan operasi lookup berdasarkan id atau url dengan kompleksitas rata-rata O(1), berbeda dengan Vec yang dapat mencapai kompleksitas O(n) dalam pencarian. Selain itu, DashMap memungkinkan penyimpanan semua data terkait dalam satu struktur, sehingga kita tidak perlu mengelola dua struktur yang berbeda seperti yang mungkin terjadi dengan Vec. Fitur dukungan akses concurrent bawaan pada DashMap juga sangat menguntungkan untuk aplikasi multi-threaded seperti BambangShop. Dengan demikian, meskipun Vec bisa digunakan, DashMap lebih unggul dalam hal kecepatan, efisiensi, dan kemudahan pemeliharaan. +<br><br> + +3. When programming using Rust, we are enforced by rigorous compiler constraints to make a thread-safe program. In the case of the List of Subscribers (SUBSCRIBERS) static variable, we used the DashMap external library for thread safe HashMap. Explain based on your understanding of design patterns, do we still need DashMap or we can implement Singleton pattern instead? + +Walaupun penggunaan Singleton (melalui lazy_static) memastikan hanya ada satu instance SUBSCRIBERS, hal tersebut tidak otomatis menjamin keamanan saat melakukan operasi concurrent pada HashMap. Singleton hanya menangani inisialisasi tunggal, sementara keamanan thread diperlukan untuk operasi baca/tulis yang aman antar-thread. DashMap, di sisi lain, menyediakan mekanisme thread-safe secara built-in tanpa perlu menambahkan lock manual seperti Mutex atau RwLock, yang bisa menambah kompleksitas dan risiko deadlock. Dengan demikian, untuk memastikan operasi yang efisien dan aman pada sistem multi-threaded seperti BambangShop, penggunaan DashMap tetap diperlukan, karena kedua konsep tersebut memiliki peran yang berbeda dan saling melengkapi. #### Reflection Publisher-2 -- GitLab