-
Notifications
You must be signed in to change notification settings - Fork 3
/
mediator.hpp
80 lines (73 loc) · 1.95 KB
/
mediator.hpp
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
#ifndef MEDIATOR_HPP
#define MEDIATOR_HPP
#include <iostream>
#include <string>
class Colleague;
class Mediator;
class ConcreteMediator;
class ConcreteColleague1;
class ConcreteColleague2;
class Mediator {
public:
virtual void Send(std::string const& message, Colleague *colleague) const = 0;
};
class Colleague {
protected:
Mediator* mediator_;
public:
explicit Colleague(Mediator *mediator):mediator_(mediator) {}
};
class ConcreteColleague1:public Colleague {
public:
explicit ConcreteColleague1(Mediator* mediator):Colleague(mediator) {}
void Send(std::string const& message) {
mediator_->Send(message, this);
}
void Notify(std::string const& message) {
std::cout << "Colleague1 gets message '" << message << "'" << std::endl;
}
};
class ConcreteColleague2:public Colleague {
public:
explicit ConcreteColleague2(Mediator *mediator):Colleague(mediator) {}
void Send(std::string const& message) {
mediator_->Send(message, this);
}
void Notify(std::string const& message) {
std::cout << "Colleague2 gets message '" << message << "'" << std::endl;
}
};
class ConcreteMediator:public Mediator {
protected:
ConcreteColleague1 *m_Colleague1;
ConcreteColleague2 *m_Colleague2;
public:
void SetColleague1(ConcreteColleague1 *c) {
m_Colleague1=c;
}
void SetColleague2(ConcreteColleague2 *c) {
m_Colleague2=c;
}
virtual void Send(std::string const& message, Colleague *colleague) const
{
if (colleague==m_Colleague1)
{
m_Colleague2->Notify(message);
}
else if (colleague==m_Colleague2)
{
m_Colleague1->Notify(message);
}
}
};
/*
ConcreteMediator m;
ConcreteColleague1 c1(&m);
ConcreteColleague2 c2(&m);
m.SetColleague1(&c1);
m.SetColleague2(&c2);
c1.Send( );
c2.Send( );
std::cin.get();
return 0;*/
#endif // MEDIATOR_HPP