-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObserver.cpp
More file actions
121 lines (99 loc) · 3.09 KB
/
Observer.cpp
File metadata and controls
121 lines (99 loc) · 3.09 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include<iostream>
#include<memory>
#include<vector>
using namespace std;
/*
Create a series of buildings, which can activate their effects simultaneisly, and change the game state.
*/
enum building_types {HOUSE, CASTLE, MINE};
//game state, contains the supply of money obtained until now
class GameState{
public:
float money;
GameState(){
money = 0;
}
void increaseMoney(int money){
this->money = money;
}
};
//all kinds of possible buildings, each one has an effect on the GameState. Building is the abstract Observer class, which contains the method "activateEffect", that will subscribe, and fired as an event
class Building{
protected:
building_types type;
float hp;
public:
Building(float hp) : hp(hp) {}
virtual void activateEffect(std::shared_ptr<GameState> gs) = 0;
};
class House : public Building{
public:
House(float hp) : Building(hp) {}
void activateEffect(std::shared_ptr<GameState> gs){
gs->money += 10;
}
};
class Castle : public Building{
public:
Castle(float hp) : Building(hp) {}
void activateEffect(std::shared_ptr<GameState> gs){
gs->money += 30;
}
};
class Mine : public Building{
public:
Mine(float hp) : Building(hp) {}
void activateEffect(std::shared_ptr<GameState> gs){
gs->money += 60;
}
};
//this is the observable class, from here you will add, remove, and notify observers
class Subject{
private:
std::vector<std::shared_ptr<Building>> observers;
public:
Subject(){
observers = {};
}
void registerObserver(std::shared_ptr<Building> b){
observers.push_back(b);
}
void unregisterObserver(std::shared_ptr<Building> b){
for(int i=0; i<observers.size(); i++){
if(b.get() == observers.at(i).get()){
observers.erase(observers.begin() + i);
break;
}
}
}
void notify(std::shared_ptr<GameState> gs){
for(int i=0; i<observers.size(); i++){
observers.at(i)->activateEffect(gs);
}
}
};
void printGameState(std::shared_ptr<GameState> gs){
cout << "Money Supply: " << gs->money << endl;
}
int main(){
std::shared_ptr<GameState> gs(new GameState());
std::shared_ptr<Subject> observable(new Subject());
std::shared_ptr<Building> house1(new House(40));
observable->registerObserver(house1);
std::shared_ptr<Building> castle1(new Castle(110));
observable->registerObserver(castle1);
std::shared_ptr<Building> mine1(new Mine(30));
observable->registerObserver(mine1);
observable->notify(gs);
printGameState(gs);
observable->unregisterObserver(house1);
observable->notify(gs);
printGameState(gs);
observable->unregisterObserver(castle1);
observable->notify(gs);
printGameState(gs);
observable->unregisterObserver(mine1);
observable->notify(gs);
printGameState(gs);
return 0;
}