-
Notifications
You must be signed in to change notification settings - Fork 2
/
observer.cpp
59 lines (51 loc) · 1.35 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
#include <iostream>
#include <list>
#include <memory>
// abstract class
class Observer {
public:
virtual void notify(const std::string&) = 0;
};
class LoggingObserver : public Observer {
public:
void notify(const std::string& value) {
std::cout << "Got notification: " << value << "\n";
}
};
class LongestStrObserver : public Observer {
private:
std::string longest_;
public:
LongestStrObserver() : longest_(""){};
const std::string& longest() { return longest_; };
void notify(const std::string& value) {
if (value.length() > longest_.length()) {
longest_ = value;
}
}
};
class ObserverManager {
public:
void registerObserver(std::shared_ptr<Observer> o) {
observers_.push_back(o);
}
void handle(const std::string& value) {
for (auto observer : observers_) {
observer->notify(value);
}
}
private:
std::list<std::shared_ptr<Observer>> observers_;
};
int main() {
std::cout << "Running observer example\n";
ObserverManager manager = ObserverManager();
manager.registerObserver(std::shared_ptr<Observer>(new LoggingObserver()));
auto longest = std::shared_ptr<LongestStrObserver>(new LongestStrObserver());
manager.registerObserver(longest);
manager.handle("test1");
manager.handle("test2");
manager.handle("test2dsds");
std::cout << "Longest str: " << longest->longest() << "\n";
return 0;
}