-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubject.h
50 lines (39 loc) · 1.54 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
50
#ifndef SUBJECT_H
#define SUBJECT_H
#include <vector>
/* A note on InfoType and StateType:
We have separated the collection of fields into two parts: its Info
and its State.
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. This is the information that the Observer "gets" when it
calls getState on the subject. It comprises the type of state, a direction,
and a colour. For more details, see state.h.
*/
// DO NOT MODIFY THIS FILE!
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