-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZooKeeper.h
119 lines (99 loc) · 3.08 KB
/
ZooKeeper.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
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
#ifndef ZOOKEEPER_H
#define ZOOKEEPER_H
#include "Subject.hpp"
#include "Observer.hpp"
#include <vector>
/*
* ZooKeeper responsibilities:
* 1 - Open Zoo
* 2 - Wake up animals
* 3 - Roll call animals
* 4 - Feed animals
* 5 - Excercise animals
* 6 - Shutdown Zoo
*/
//ZooKeeper is the subject for observer pattern
class ZooKeeper : public Subject{
public:
ZooKeeper(string name, Zoo* work){
this->name = name;
this->workplace = work;
}
virtual void registerObserver(Observer *o) override {
observers.push_back(o);
}
virtual void removeObserver(Observer *o) override {
for(unsigned int i = 0; i < observers.size(); i++){
Observer* temp = observers[i];
if(temp == o){
observers.erase(observers.begin() + i);
}
}
}
//Calls update in all Observers
virtual void notifyObservers() override {
for(unsigned int i = 0; i < observers.size(); i++){
Observer* temp = observers[i];
temp->update(event);
}
}
void openZoo(){
event = "open the zoo.";
notifyObservers();
cout<<"Zookeeper " + name + " is opening the Zoo!\n";
//Open the Zoo
workplace->openZoo();
}
void closeZoo(){
event = "close the zoo.";
notifyObservers();
cout<<"Zookeeper " + name + " is closing the Zoo.\n";
for(unsigned int i=0; i<workplace->zooAnimals.size(); i++){
//We have an animal pointer remember...
workplace->zooAnimals[i]->sleep();
}
//Close Zoo!
workplace->closeZoo();
}
void wakeUpAnimals(){
event = "wake up the animals.";
notifyObservers();
for(unsigned int i=0; i<workplace->zooAnimals.size(); i++){
//We have an animal pointer remember...
workplace->zooAnimals[i]->wakeUp();
}
}
void rollCall(){
event = "do the roll call.";
notifyObservers();
cout<<"Zookeeper " + name + " is now doing roll call!\n";
for(unsigned int i=0; i<workplace->zooAnimals.size(); i++){
//We have an animal pointer remember...
workplace->zooAnimals[i]->makeNoise();
}
}
void feedAnimals(){
event = "feed the animals.";
notifyObservers();
cout<<"Zookeeper " + name + " is feeding Animals!\n";
for(unsigned int i=0; i<workplace->zooAnimals.size(); i++){
//We have an animal pointer remember...
workplace->zooAnimals[i]->eat();
}
}
void exerciseAnimals(){
event = "exercise the animals.";
notifyObservers();
cout<<"Zookeeper " + name + " is exercising Animals!\n";
for(unsigned int i=0; i<workplace->zooAnimals.size(); i++){
//We have an animal pointer remember...
workplace->zooAnimals[i]->roam();
}
}
private:
string name;
Zoo* workplace; //The Zoo that ZooKeeper works at
vector<Observer*> observers; //Observer Pattern
string event;
};
#endif // ZOOKEEPER_H