-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobserver.cpp
More file actions
65 lines (54 loc) · 1.23 KB
/
observer.cpp
File metadata and controls
65 lines (54 loc) · 1.23 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
#include <iostream>
#include "observer.h"
#include "observer_new.h"
#include <string>
#include "event_type.h"
void foo(int i)
{
std::cout << "foo( " << i << " )\n";
}
void bar()
{
std::cout << "bar()\n";
}
void test_observer_1()
{
Subject<EventType> s;
s.registerObserver(EventType::GREEN, bar);
s.registerObserver(EventType::ORANGE, std::bind(foo, 42));
s.registerObserver(EventType::RED, std::bind(foo, 12345));
s.notify(EventType::GREEN);
s.notify(EventType::RED);
s.notify(EventType::ORANGE);
}
void test_observer_2()
{
Subject<std::string> s;
s.registerObserver("GREEN", bar);
s.registerObserver("ORANGE", std::bind(foo, 42));
s.registerObserver("RED", std::bind(foo, 12345));
const std::string msg("hello world");
s.registerObserver("RED", [&msg]{std::cout << msg << std::endl;});
s.notify("GREEN");
s.notify("RED");
s.notify("ORANGE");
}
int main(int argc, char **argv)
{
// int total = 0;
//
// ICartVisitor *cart = new CartVisitor();
// Book book;
// Pen pen;
// Computer computer;
//
// total += cart->Visit(book);
// total += cart->Visit(pen);
// total += cart->Visit(computer);
//
// std::cout << total << std::endl;
// // 5510
test_observer_1();
// test_observer_2();
return 0;
}