-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathmain.cpp
64 lines (55 loc) · 1.49 KB
/
main.cpp
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
#include <iostream>
#include <string>
#include <vector>
#include <functional>
class JobPost {
public:
JobPost(const std::string& title): title_(title) {}
const std::string& GetTitle() const { return title_; }
private:
std::string title_;
};
class IObserver {
public:
virtual void OnJobPosted(const JobPost& job) = 0;
};
class JobSeeker : public IObserver {
public:
JobSeeker(const std::string& name): name_(name) {}
void OnJobPosted(const JobPost &job) override {
std::cout << "Hi " << name_ << "! New job posted: " << job.GetTitle() << std::endl;
}
private:
std::string name_;
};
class IObservable {
public:
virtual void Attach(IObserver& observer) = 0;
virtual void AddJob(const JobPost& jobPosting) = 0;
protected:
virtual void Notify(const JobPost& jobPosting) = 0;
};
class JobPostings : public IObservable {
public:
void Attach(IObserver& observer) override {
observers_.push_back(observer);
}
void AddJob(const JobPost &jobPosting) override {
Notify(jobPosting);
}
private:
void Notify(const JobPost &jobPosting) override {
for (IObserver& observer : observers_)
observer.OnJobPosted(jobPosting);
}
std::vector<std::reference_wrapper<IObserver>> observers_;
};
int main()
{
JobSeeker johnDoe("John Doe");
JobSeeker janeDoe("Jane Doe");
JobPostings jobPostings;
jobPostings.Attach(johnDoe);
jobPostings.Attach(janeDoe);
jobPostings.AddJob(JobPost("Software Engineer"));
}