v1 using observer pattern
This commit is contained in:
@@ -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;
|
||||
};
|
||||
Reference in New Issue
Block a user