-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathobserver.cpp
82 lines (69 loc) · 2.07 KB
/
observer.cpp
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
80
81
82
// Observer design pattern
#include <cstddef>
#include <iostream>
#include <map>
#include <memory>
#include <vector>
// observer interface
struct IObserver {
virtual void notify() const = 0;
virtual ~IObserver() = default;
};
// concrete observer
class Observer : public IObserver {
std::size_t _ID;
public:
explicit Observer(std::size_t ID) : _ID{ID} {}
void notify() const override {
std::cout << "\tObserver " << _ID << " notified!\n";
}
std::size_t ID() const { return _ID; }
};
// subject interface
struct ISubject {
virtual void registerObserver(std::shared_ptr<Observer> spo) = 0;
virtual void unregisterObserver(std::shared_ptr<Observer> spo) = 0;
virtual void notifyObservers() const = 0;
virtual ~ISubject() = default;
};
// concrete subject
class Subject : public ISubject {
std::map<std::size_t, std::shared_ptr<Observer>> _observers{};
public:
void registerObserver(std::shared_ptr<Observer> spo) override {
_observers[spo->ID()] = spo;
}
void unregisterObserver(std::shared_ptr<Observer> spo) override {
_observers.erase(spo->ID());
}
void notifyObservers() const override {
for (auto& elem : _observers) {
elem.second->notify();
}
}
};
int main() {
// 1 subject
Subject subject;
// N observers
std::size_t N = 4;
std::vector<std::shared_ptr<Observer>> vobs;
for (std::size_t i = 0; i < N; ++i) {
vobs.push_back(std::make_shared<Observer>(i));
}
// register all of them
std::cout << "Registering all " << N << " Observers...\n";
for (std::size_t i = 0; i < vobs.size(); ++i) {
subject.registerObserver(vobs[i]);
}
// notify
std::cout << "Notifying all " << N << " Observers...\n";
subject.notifyObservers();
// un-register Observers 1 and 2
std::cout << "Un-registering Observer 1 and Observer 2...\n";
subject.unregisterObserver(vobs[1]);
subject.unregisterObserver(vobs[2]);
// notify
std::cout << "Notifying remaining Observers:\n";
subject.notifyObservers();
}