-
Notifications
You must be signed in to change notification settings - Fork 0
/
subject.h
executable file
·49 lines (39 loc) · 1.21 KB
/
subject.h
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
#ifndef SUBJECT_H
#define SUBJECT_H
#include <vector>
/*
Info is for the parts of the subjject inherent to what it is: its position
and its colour.
State is for the parts of the subject that, when changed, trigger
notifications.
*/
template <typename InfoType, typename StateType> class Observer;
template <typename InfoType, typename StateType> class Subject {
std::vector<Observer<InfoType, StateType> *> observers;
StateType state;
protected:
void setState(StateType newS);
public:
void attach(Observer<InfoType, StateType> *o);
void notifyObservers();
virtual InfoType getInfo() const = 0;
StateType getState() const;
};
template <typename InfoType, typename StateType>
void Subject<InfoType, StateType>::attach(Observer<InfoType, StateType> *o) {
observers.emplace_back(o);
}
template <typename InfoType, typename StateType>
void Subject<InfoType, StateType>::notifyObservers() {
for (auto &ob : observers)
ob->notify(*this);
}
template <typename InfoType, typename StateType>
void Subject<InfoType, StateType>::setState(StateType newS) {
state = newS;
}
template <typename InfoType, typename StateType>
StateType Subject<InfoType, StateType>::getState() const {
return state;
}
#endif