-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbridge.cpp
117 lines (102 loc) · 2.14 KB
/
bridge.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
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
//将抽象部分和实现部分分离,使它们可以独立的变化
//多个变化维度,不同平台和不同功能
#include<string>
#include<iostream>
class MessagerImp;
class Messager
{
public:
MessagerImp* messagerImp;
Messager(MessagerImp* mimp) :messagerImp(mimp)
{
}
virtual void Login(std::string username, std::string password) = 0;
virtual void SendMessage(std::string message) = 0;
virtual ~Messager() {}
};
class MessagerImp
{
public:
virtual void PlaySound() = 0;
virtual void WriteText() = 0;
virtual void Connect() = 0;
virtual ~MessagerImp() {}
};
//平台实现
class PCMessagerImp : public MessagerImp
{
public:
virtual void PlaySound()
{
std::cout << "PC PlaySound" << std::endl;
}
virtual void WriteText()
{
std::cout << "PC WriteText" << std::endl;
}
virtual void Connect()
{
std::cout << "PC Connect" << std::endl;
}
};
class MobileMessagerImp :public MessagerImp
{
public:
virtual void PlaySound()
{
std::cout << "Mobile PlaySound" << std::endl;
}
virtual void WriteText()
{
std::cout << "Mobile WriteText" << std::endl;
}
virtual void Connect()
{
std::cout << "Mobile Connect" << std::endl;
}
};
class MessagerLite : public Messager
{
public:
MessagerLite(MessagerImp* mimp) :Messager(mimp)
{
}
virtual void Login(std::string username, std::string password)
{
messagerImp->Connect();
}
virtual void SendMessage(std::string message)
{
messagerImp->WriteText();
}
};
class MessagerPerfect : public Messager
{
public:
MessagerPerfect(MessagerImp* mimp) :Messager(mimp)
{
}
virtual void Login(std::string username, std::string password)
{
messagerImp->Connect();
messagerImp->PlaySound();
}
virtual void SendMessage(std::string message)
{
messagerImp->WriteText();
messagerImp->PlaySound();
}
};
//int main()
//{
// MessagerImp* mImp = new PCMessagerImp();
// Messager* m = new MessagerLite(mImp);
// m->Login("g", "s");
// m->SendMessage("hello");
//
// //
// MessagerImp* mImp2 = new MobileMessagerImp();
// Messager* m2= new MessagerLite(mImp2);
// m2->Login("g", "s");
// m2->SendMessage("hello");
//}