-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathMain.cpp
More file actions
38 lines (28 loc) · 715 Bytes
/
Main.cpp
File metadata and controls
38 lines (28 loc) · 715 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
#include "Subject.hpp"
#include <iostream>
using namespace std;
struct State {
int data;
};
void handleStateChange(const State& newState)
{
cout << "State changed to: " << newState.data << endl;
}
int main()
{
// Create a new subject with a certain state.
Subject<State> someSubject;
// Attach the observer, this can be any function.
// Multiple observers can have the same key.
// Or no key at all.
someSubject.attach(handleStateChange, "stateHandle");
// Get the state and change it.
State state = someSubject.getState();
state.data = 1337;
someSubject.setState(state);
// Detach the observer
someSubject.detach("stateHandle");
state.data = 12;
someSubject.setState(state);
return 0;
}