From 411d49bb7ce819762148a7c7eafcb51f48edd0dc Mon Sep 17 00:00:00 2001 From: ruinivist Date: Sun, 11 Jan 2026 23:19:44 +0000 Subject: [PATCH] use shared ptr, makr fn const --- subscriber-notifs/include/core/subscribable.hpp | 14 +++++++++----- subscriber-notifs/src/app/inventory_manager.cpp | 5 ++++- subscriber-notifs/src/main.cpp | 14 ++++++++------ 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/subscriber-notifs/include/core/subscribable.hpp b/subscriber-notifs/include/core/subscribable.hpp index ad9a131..dc47bc5 100644 --- a/subscriber-notifs/include/core/subscribable.hpp +++ b/subscriber-notifs/include/core/subscribable.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -10,14 +11,17 @@ class Subscribable { using TSubscriber = Subscriber; private: - std::unordered_map> subscribersMap_; + std::unordered_map>> + subscribersMap_; public: - void subscribe(const TEventId& eventId, TSubscriber* subscriber) { + void subscribe(const TEventId& eventId, + std::shared_ptr subscriber) { subscribersMap_[eventId].push_back(subscriber); } - void unsubscribe(const TEventId& eventId, TSubscriber* subscriber) { + void unsubscribe(const TEventId& eventId, + std::shared_ptr subscriber) { auto entry = subscribersMap_.find(eventId); if (entry == subscribersMap_.end()) { return; @@ -29,13 +33,13 @@ class Subscribable { } } - void notify(const TEventId& eventId, const TEventInfo& eventInfo) { + void notify(const TEventId& eventId, const TEventInfo& eventInfo) const { auto entry = subscribersMap_.find(eventId); if (entry == subscribersMap_.end()) { return; } - for (TSubscriber* subscriber : entry->second) { + for (auto& subscriber : entry->second) { subscriber->onNotification(eventId, eventInfo); } } diff --git a/subscriber-notifs/src/app/inventory_manager.cpp b/subscriber-notifs/src/app/inventory_manager.cpp index 7962c68..4e5f673 100644 --- a/subscriber-notifs/src/app/inventory_manager.cpp +++ b/subscriber-notifs/src/app/inventory_manager.cpp @@ -2,6 +2,9 @@ void InventoryManager::setInventory(const ProductId& productId, ProductCount quantity) { + ProductCount oldQuantity = inventory_[productId]; inventory_[productId] = quantity; - this->notify(productId, quantity); + if (oldQuantity != quantity) { + this->notify(productId, quantity); + } } \ No newline at end of file diff --git a/subscriber-notifs/src/main.cpp b/subscriber-notifs/src/main.cpp index 65e7368..e3b9582 100644 --- a/subscriber-notifs/src/main.cpp +++ b/subscriber-notifs/src/main.cpp @@ -1,16 +1,18 @@ +#include + #include "app/inventory_manager.hpp" #include "app/services.hpp" int main() { InventoryManager inventoryManager; - SmsService smsService; - EmailService emailService; - LogService logService; + auto smsService = std::make_shared(); + auto emailService = std::make_shared(); + auto logService = std::make_shared(); - inventoryManager.subscribe("ProductA", &smsService); - inventoryManager.subscribe("ProductB", &emailService); - inventoryManager.subscribe("ProductA", &logService); + inventoryManager.subscribe("ProductA", smsService); + inventoryManager.subscribe("ProductB", emailService); + inventoryManager.subscribe("ProductA", logService); inventoryManager.setInventory("ProductA", 100); inventoryManager.setInventory("ProductB", 200);