pub sub C++代码
时间: 2023-09-12 11:10:23 浏览: 64
以下是一个简单的 C++ 实现的 Pub/Sub 模式代码:
```cpp
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
using namespace std;
class Subscriber {
public:
virtual void update(const string& message) = 0;
};
class Publisher {
private:
unordered_map<string, vector<Subscriber*>> subscribers;
public:
void subscribe(const string& topic, Subscriber* subscriber) {
subscribers[topic].push_back(subscriber);
}
void unsubscribe(const string& topic, Subscriber* subscriber) {
auto it = subscribers.find(topic);
if (it != subscribers.end()) {
auto& subList = it->second;
subList.erase(remove(subList.begin(), subList.end(), subscriber), subList.end());
}
}
void publish(const string& topic, const string& message) {
auto it = subscribers.find(topic);
if (it != subscribers.end()) {
auto& subList = it->second;
for (auto sub : subList) {
sub->update(message);
}
}
}
};
class EmailSubscriber : public Subscriber {
public:
void update(const string& message) override {
cout << "Email received: " << message << endl;
}
};
class SMSSubscriber : public Subscriber {
public:
void update(const string& message) override {
cout << "SMS received: " << message << endl;
}
};
int main() {
Publisher publisher;
EmailSubscriber* emailSub1 = new EmailSubscriber();
EmailSubscriber* emailSub2 = new EmailSubscriber();
SMSSubscriber* smsSub1 = new SMSSubscriber();
SMSSubscriber* smsSub2 = new SMSSubscriber();
publisher.subscribe("news", emailSub1);
publisher.subscribe("news", emailSub2);
publisher.subscribe("news", smsSub1);
publisher.subscribe("weather", smsSub2);
publisher.publish("news", "Breaking news: the world is round!");
publisher.publish("weather", "Today's weather forecast: sunny!");
publisher.unsubscribe("news", emailSub2);
publisher.publish("news", "More news: water is wet!");
delete emailSub1;
delete emailSub2;
delete smsSub1;
delete smsSub2;
return 0;
}
```
这里我们定义了 `Subscriber` 抽象类和 `EmailSubscriber` 和 `SMSSubscriber` 两个具体的订阅者类。`Publisher` 类中使用了一个哈希表来记录订阅者们订阅的话题,然后在 `subscribe` 和 `unsubscribe` 函数中来操作哈希表。在 `publish` 函数中,我们找到对应话题的订阅者列表,然后遍历列表并调用每个订阅者的 `update` 函数来通知它们。在 `main` 函数中,我们创建了几个订阅者和一个发布者,并演示了如何订阅、发布和取消订阅。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)