-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathobserver.cpp
More file actions
43 lines (37 loc) · 774 Bytes
/
observer.cpp
File metadata and controls
43 lines (37 loc) · 774 Bytes
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
#include <iostream>
#include <string>
class Observer
{
public:
virtual ~Observer() { }
virtual void on_message(const std::string &)=0;
};
class MessageBoard
{
Observer & observer;
public:
MessageBoard(Observer & obs) : observer(obs) { }
void post(const std::string & str)
{
observer.on_message(str);
};
};
class Writer : public Observer
{
public:
void on_message(const std::string & str)
{
std::cout << str << std::endl;
}
};
void hello_world(MessageBoard & message_board)
{
message_board.post("Hello world!");
}
int main()
{
Writer writer;
MessageBoard message_board(writer);
hello_world(message_board);
return 0;
}