-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobserver_smartPtr_bad.cpp
More file actions
79 lines (64 loc) · 1.99 KB
/
Copy pathobserver_smartPtr_bad.cpp
File metadata and controls
79 lines (64 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// this is bad way to use smart pointers
// neither subject nor object should own one another
#include <algorithm>
#include <iostream>
#include <list>
#include <memory>
#include <string>
class ISubscriber {
public:
// Pure virtual function to make the class abstract
virtual void update(const std::string &) = 0;
// prevents delete via base constructor unambiguous
virtual ~ISubscriber() = default;
};
class IPublisher {
public:
// method to subscribe
virtual void subscribe(const std::weak_ptr<ISubscriber> &) = 0;
// method to unsubscribe
virtual void unsubscribe(const std::shared_ptr<ISubscriber> &) = 0;
// method to notify all Subscribers about the change
virtual void notifySubscribers() = 0;
virtual ~IPublisher() = default;
protected:
std::list<std::shared_ptr<ISubscriber>> subs;
};
class Subscriber : public ISubscriber {
public:
void update(const std::string &msg) override {
std::cout << this->id << " " << msg << std::endl;
}
Subscriber(int sid) : id(sid) {}
private:
int id;
};
class Publisher : public IPublisher {
public:
void subscribe(const std::weak_ptr<ISubscriber> &subscriber) override {
if (auto sub = subscriber.lock())
subs.push_front(sub);
}
void unsubscribe(const std::shared_ptr<ISubscriber> &sub) override {
auto itr = std::find(subs.begin(), subs.end(), sub);
if (itr != subs.end())
subs.erase(itr);
}
void notifySubscribers() override {
for (const auto &sub : subs)
sub->update("update message");
}
};
int main() {
Publisher obs;
std::shared_ptr<Subscriber> s1 = std::make_shared<Subscriber>(1);
std::shared_ptr<Subscriber> s2 = std::make_shared<Subscriber>(2);
std::shared_ptr<Subscriber> s3 = std::make_shared<Subscriber>(3);
obs.subscribe(s1);
obs.subscribe(s2);
obs.subscribe(s3);
obs.notifySubscribers();
obs.unsubscribe(s2);
obs.notifySubscribers();
return 0;
}