-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathraw_observer.cpp
More file actions
79 lines (64 loc) · 1.71 KB
/
Copy pathraw_observer.cpp
File metadata and controls
79 lines (64 loc) · 1.71 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
#include "observable.h"
#include "observer.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
/**
* Observable: Person notifies when the age field is changed.
*/
class Person : public Observable<Person> {
int age{0};
public:
Person() {}
explicit Person(int age) : age(age) {}
int get_age() const { return age; }
void set_age(int age) {
if (this->age == age)
return;
auto old_can_vote = get_can_vote(); // (1)
this->age = age;
notify(*this, "age");
if (old_can_vote != get_can_vote()) // (2)
notify(*this, "can_vote"); // (3)
}
/**
* This "class attribute" shows a complication of the observer pattern, where
* it is not easy to notify changes on fields which depend on others, and have
* no setter.
*
* See how the set_age() method turns complex when dealing with this
* attribute:
* - (1): keep old state.
* - (2): assess diff with new state.
* - (3): notify if necessary.
*/
bool get_can_vote() const { return age >= 16; }
};
/**
* Observer: Console observes age changes and prints them.
*/
struct ConsolePersonObserver : public Observer<Person> {
private:
// implements field_changed() callback
void field_changed(Person &source, const std::string &field_name) override {
cout << "[ConsolePersonObserver] Person's " << field_name
<< " has changed to ";
if (field_name == "age")
cout << source.get_age();
if (field_name == "can_vote")
cout << boolalpha << source.get_can_vote();
cout << ".\n";
}
};
int main() {
Person p;
ConsolePersonObserver cpo;
p.subscribe(cpo);
p.set_age(15);
p.set_age(16);
p.set_age(17);
p.unsubscribe(cpo);
p.set_age(18);
return 0;
}