v1 using observer pattern
This commit is contained in:
@@ -0,0 +1,2 @@
|
|||||||
|
BasedOnStyle: Google
|
||||||
|
IndentWidth: 4
|
||||||
+59
@@ -0,0 +1,59 @@
|
|||||||
|
# CMake generated files
|
||||||
|
CMakeCache.txt
|
||||||
|
CMakeFiles/
|
||||||
|
cmake_install.cmake
|
||||||
|
Makefile
|
||||||
|
*.cmake
|
||||||
|
compile_commands.json
|
||||||
|
|
||||||
|
# Build directories
|
||||||
|
build/
|
||||||
|
cmake-build-*/
|
||||||
|
out/
|
||||||
|
bin/
|
||||||
|
lib/
|
||||||
|
|
||||||
|
# C++ build artifacts
|
||||||
|
*.o
|
||||||
|
*.obj
|
||||||
|
*.exe
|
||||||
|
*.dll
|
||||||
|
*.so
|
||||||
|
*.dylib
|
||||||
|
*.a
|
||||||
|
*.lib
|
||||||
|
|
||||||
|
# IDE and editor files
|
||||||
|
.vscode/settings.json
|
||||||
|
*.swp
|
||||||
|
*.swo
|
||||||
|
*~
|
||||||
|
.idea/
|
||||||
|
*.sublime-project
|
||||||
|
*.sublime-workspace
|
||||||
|
|
||||||
|
# OS generated files
|
||||||
|
.DS_Store
|
||||||
|
.DS_Store?
|
||||||
|
._*
|
||||||
|
.Spotlight-V100
|
||||||
|
.Trashes
|
||||||
|
ehthumbs.db
|
||||||
|
Thumbs.db
|
||||||
|
|
||||||
|
# Temporary files
|
||||||
|
*.tmp
|
||||||
|
*.temp
|
||||||
|
*.log
|
||||||
|
|
||||||
|
# Package files
|
||||||
|
*.deb
|
||||||
|
*.rpm
|
||||||
|
*.tar.gz
|
||||||
|
*.zip
|
||||||
|
|
||||||
|
# cache
|
||||||
|
.cache
|
||||||
|
|
||||||
|
# binary
|
||||||
|
sub_notifs
|
||||||
@@ -4,11 +4,11 @@ project(subscriber-notifs VERSION 1.0)
|
|||||||
set(CMAKE_CXX_STANDARD 20)
|
set(CMAKE_CXX_STANDARD 20)
|
||||||
|
|
||||||
file(GLOB_RECURSE SRC_FILES "src/*.cpp")
|
file(GLOB_RECURSE SRC_FILES "src/*.cpp")
|
||||||
file(GLOB_RECURSE HEADER_FILES "src/*.h" "src/*.hpp")
|
file(GLOB_RECURSE HEADER_FILES "include/*.hpp")
|
||||||
|
|
||||||
add_executable(sub_notifs ${SRC_FILES} ${HEADER_FILES})
|
add_executable(sub_notifs ${SRC_FILES} ${HEADER_FILES})
|
||||||
|
|
||||||
target_include_directories(sub_notifs PRIVATE ${HEADER_FILES})
|
target_include_directories(sub_notifs PRIVATE include )
|
||||||
|
|
||||||
# from: https://cmake.org/cmake/help/latest/module/FindThreads.html
|
# from: https://cmake.org/cmake/help/latest/module/FindThreads.html
|
||||||
find_package(Threads REQUIRED)
|
find_package(Threads REQUIRED)
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
|
#include "core/subscribable.hpp"
|
||||||
|
|
||||||
|
using ProductCount = int;
|
||||||
|
using ProductId = std::string;
|
||||||
|
|
||||||
|
// inventory manager is not generic ( works with the specific types ProductId
|
||||||
|
// and ProductCount ) but the subscribable is generic
|
||||||
|
class InventoryManager : public Subscribable<ProductId, ProductCount> {
|
||||||
|
private:
|
||||||
|
std::unordered_map<ProductId, int> inventory_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
void setInventory(const ProductId& productId, ProductCount quantity);
|
||||||
|
};
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "app/inventory_manager.hpp"
|
||||||
|
#include "core/subscriber.hpp"
|
||||||
|
|
||||||
|
class SmsService : public Subscriber<ProductId, ProductCount> {
|
||||||
|
public:
|
||||||
|
void onNotification(const ProductId& productId,
|
||||||
|
const ProductCount& quantity) override;
|
||||||
|
};
|
||||||
|
|
||||||
|
class EmailService : public Subscriber<ProductId, ProductCount> {
|
||||||
|
public:
|
||||||
|
void onNotification(const ProductId& productId,
|
||||||
|
const ProductCount& quantity) override;
|
||||||
|
};
|
||||||
|
|
||||||
|
class LogService : public Subscriber<ProductId, ProductCount> {
|
||||||
|
public:
|
||||||
|
void onNotification(const ProductId& productId,
|
||||||
|
const ProductCount& quantity) override;
|
||||||
|
};
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "subscriber.hpp"
|
||||||
|
|
||||||
|
template <typename TEventId, typename TEventInfo>
|
||||||
|
class Subscribable {
|
||||||
|
using TSubscriber = Subscriber<TEventId, TEventInfo>;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::unordered_map<TEventId, std::vector<TSubscriber*>> subscribersMap_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
void subscribe(const TEventId& eventId, TSubscriber* subscriber) {
|
||||||
|
subscribersMap_[eventId].push_back(subscriber);
|
||||||
|
}
|
||||||
|
|
||||||
|
void unsubscribe(const TEventId& eventId, TSubscriber* subscriber) {
|
||||||
|
auto entry = subscribersMap_.find(eventId);
|
||||||
|
if (entry == subscribersMap_.end()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::erase(entry->second, subscriber);
|
||||||
|
if (entry->second.empty()) {
|
||||||
|
subscribersMap_.erase(entry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void notify(const TEventId& eventId, const TEventInfo& eventInfo) {
|
||||||
|
auto entry = subscribersMap_.find(eventId);
|
||||||
|
if (entry == subscribersMap_.end()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (TSubscriber* subscriber : entry->second) {
|
||||||
|
subscriber->onNotification(eventId, eventInfo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
template <typename TEventId, typename TEventInfo>
|
||||||
|
class Subscriber {
|
||||||
|
public:
|
||||||
|
virtual void onNotification(const TEventId& eventId,
|
||||||
|
const TEventInfo& eventInfo) = 0;
|
||||||
|
|
||||||
|
virtual ~Subscriber() = default;
|
||||||
|
};
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
#include "app/inventory_manager.hpp"
|
||||||
|
|
||||||
|
void InventoryManager::setInventory(const ProductId& productId,
|
||||||
|
ProductCount quantity) {
|
||||||
|
inventory_[productId] = quantity;
|
||||||
|
this->notify(productId, quantity);
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
#include "app/services.hpp"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
void SmsService::onNotification(const ProductId& productId,
|
||||||
|
const ProductCount& quantity) {
|
||||||
|
std::cout << "SMS Notification - Product: " << productId
|
||||||
|
<< ", Quantity: " << quantity << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void EmailService::onNotification(const ProductId& productId,
|
||||||
|
const ProductCount& quantity) {
|
||||||
|
std::cout << "Email Notification - Product: " << productId
|
||||||
|
<< ", Quantity: " << quantity << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void LogService::onNotification(const ProductId& productId,
|
||||||
|
const ProductCount& quantity) {
|
||||||
|
std::cout << "Log Entry - Product: " << productId
|
||||||
|
<< ", Quantity: " << quantity << std::endl;
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
#include "app/inventory_manager.hpp"
|
||||||
|
#include "app/services.hpp"
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
InventoryManager inventoryManager;
|
||||||
|
|
||||||
|
SmsService smsService;
|
||||||
|
EmailService emailService;
|
||||||
|
LogService logService;
|
||||||
|
|
||||||
|
inventoryManager.subscribe("ProductA", &smsService);
|
||||||
|
inventoryManager.subscribe("ProductB", &emailService);
|
||||||
|
inventoryManager.subscribe("ProductA", &logService);
|
||||||
|
|
||||||
|
inventoryManager.setInventory("ProductA", 100);
|
||||||
|
inventoryManager.setInventory("ProductB", 200);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user