-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathobserver.cpp
More file actions
67 lines (57 loc) · 1.5 KB
/
observer.cpp
File metadata and controls
67 lines (57 loc) · 1.5 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
// Observer
// C++11
#include <vector>
#include <functional>
class observer
{
public:
virtual void notify() = 0;
};
class observer_concrete : public observer
{
public:
virtual void notify() override
{ }
};
class subject
{
public:
void register_observer(observer& o)
{
observers.push_back(o);
}
void notify_observers()
{
for (observer& o : observers) {
o.notify();
}
}
private:
std::vector<std::reference_wrapper<observer>> observers;
};
// Notify generic observer objects when an event occurs.
//
// The observer pattern allows generic observer objects to be
// registered with a subject object and receive notifications
// when certain events occur.
//
// The `subject` class, defined on [20-37], contains a
// [`std::vector`](cpp/container/vector) of references to observers [36].
// Observers (also known as listeners), in this case, are objects that
// implement the `observer` interface ([7-11]). The
// `register_observer` function ([23-26]) adds observers
// to this `std::vector`, which are later to be notified by the
// `notify_observers` function ([28-33]).
//
// We use [`std::reference_wrapper`](cpp/utility/functional/reference_wrapper)
// for the elements of the `std::vector` ([36]), because the standard
// containers require the element type to be assignable, which normal
// reference types are not.
int main()
{
subject s;
observer_concrete o1, o2;
s.register_observer(o1);
s.register_observer(o2);
s.notify_observers();
}