-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObserverYoutube.cpp
More file actions
70 lines (56 loc) · 1.38 KB
/
Copy pathObserverYoutube.cpp
File metadata and controls
70 lines (56 loc) · 1.38 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
#include <iostream>
#include <vector>
// Observer Interface
class IObserver {
public:
virtual void Update() = 0;
};
// Channel Class
class Channel {
private:
std::vector<IObserver*> observers;
std::string title;
public:
void Subscribe(IObserver* observer) {
observers.push_back(observer);
}
void UnSubscribe(IObserver* observer) {
observers.erase(std::remove(observers.begin(), observers.end(), observer), observers.end());
}
void NotifyObservers() {
for (IObserver* observer : observers) {
observer->Update();
}
}
void Upload(std::string title) {
this->title = title;
NotifyObservers();
}
};
// Subscriber Class
class Subscriber : public IObserver {
private:
std::string name;
Channel* channel;
public:
Subscriber(std::string name) : name(name), channel(nullptr) {}
void Update() override {
std::cout << "Hey, " << name << " Video Updated.." << channel->title << std::endl;
}
void SubscribeChannel(Channel* ch) {
channel = ch;
}
};
// Main Function
int main() {
Channel samyam;
Subscriber s1("Boult");
Subscriber s2("Southee");
samyam.Subscribe(&s1);
samyam.Subscribe(&s2);
samyam.UnSubscribe(&s1);
s1.SubscribeChannel(&samyam);
s2.SubscribeChannel(&samyam);
samyam.Upload("How to swing the ball?");
return 0;
}