-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdapterPattern.cpp
More file actions
204 lines (165 loc) · 4.69 KB
/
Copy pathAdapterPattern.cpp
File metadata and controls
204 lines (165 loc) · 4.69 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**************************************************************************
* 适配器模式
* 意图:将一个类的接口转换成客户希望的另外一个接口。适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
主要解决:主要解决在软件系统中,常常要将一些"现存的对象"放到新的环境中,而新环境要求的接口是现对象不能满足的。
何时使用: 1、系统需要使用现有的类,而此类的接口不符合系统的需要。 2、想要建立一个可以重复使用的类,用于与一些彼此之间没有太大关联的一些类,包括一些可能在将来引进的类一起工作,这些源类不一定有一致的接口。 3、通过接口转换,将一个类插入另一个类系中。(比如老虎和飞禽,现在多了一个飞虎,在不增加实体的需求下,增加一个适配器,在里面包容一个虎对象,实现飞的接口。)
如何解决:继承或依赖(推荐)。
关键代码:适配器继承或依赖已有的对象,实现想要的目标接口。
应用实例: 1、美国电器 110V,中国 220V,就要有一个适配器将 110V 转化为 220V。 2、JAVA JDK 1.1 提供了 Enumeration 接口,而在 1.2 中提供了 Iterator 接口,想要使用 1.2 的 JDK,则要将以前系统的 Enumeration 接口转化为 Iterator 接口,这时就需要适配器模式。 3、在 LINUX 上运行 WINDOWS 程序。 4、JAVA 中的 jdbc。
优点: 1、可以让任何两个没有关联的类一起运行。 2、提高了类的复用。 3、增加了类的透明度。 4、灵活性好。
缺点: 1、过多地使用适配器,会让系统非常零乱,不易整体进行把握。比如,明明看到调用的是 A 接口,其实内部被适配成了 B 接口的实现,一个系统如果太多出现这种情况,无异于一场灾难。因此如果不是很有必要,可以不使用适配器,而是直接对系统进行重构。 2.由于 JAVA 至多继承一个类,所以至多只能适配一个适配者类,而且目标类必须是抽象类。
使用场景:有动机地修改一个正常运行的系统的接口,这时应该考虑使用适配器模式。
注意事项:适配器不是在详细设计时添加的,而是解决正在服役的项目的问题。
**************************************************************************/
#include <iostream>
#include <string>
using namespace std;
class MediaPlayer
{
public:
MediaPlayer() = default;
~MediaPlayer() = default;
virtual void play(string audioTyope, string fileName) {};
private:
};
class AdvancedMediaPlayer
{
public:
AdvancedMediaPlayer() = default;
~AdvancedMediaPlayer() = default;
virtual void playVlc(string fileName) {};
virtual void playMp4(string fileName) {};
};
class VlcPlayer : public AdvancedMediaPlayer
{
public:
VlcPlayer() = default;
~VlcPlayer() = default;
void playVlc(string fileName) override
{
cout << ("Playing vlc file. Name: " + fileName) << endl;
}
void playMp4(string fileName) override
{
//什么也不做
}
};
class Mp4Player : public AdvancedMediaPlayer {
public:
Mp4Player() = default;
~Mp4Player() = default;
void playVlc(string fileName) override
{
//什么也不做
}
void playMp4(string fileName) override
{
cout << ("Playing mp4 file. Name: " + fileName);
}
};
class MediaAdapter : public MediaPlayer {
public:
MediaAdapter(string audioType) {
if (audioType._Equal("vlc")) {
advancedMusicPlayer = new VlcPlayer();
}
else if (audioType._Equal("mp4")) {
advancedMusicPlayer = new Mp4Player();
}
}
void play(string audioType, string fileName) override {
if (audioType._Equal("vlc")) {
advancedMusicPlayer->playVlc(fileName);
}
else if (audioType._Equal("mp4")) {
advancedMusicPlayer->playMp4(fileName);
}
}
private:
AdvancedMediaPlayer* advancedMusicPlayer = nullptr;
};
class AudioPlayer : public MediaPlayer {
public:
void play(string audioType, string fileName) override
{
//播放 mp3 音乐文件的内置支持
if (audioType._Equal("mp3"))
{
cout << ("Playing mp3 file. Name: " + fileName) << endl;
}
//mediaAdapter 提供了播放其他文件格式的支持
else if (audioType._Equal("vlc")
|| audioType._Equal("mp4"))
{
mediaAdapter = new MediaAdapter(audioType);
mediaAdapter->play(audioType, fileName);
}
else
{
cout << ("Invalid media. " +
audioType + " format not supported") << endl;
}
}
private:
MediaAdapter* mediaAdapter = nullptr;
};
//class person
//{
//public:
// string name = "myName";
//protected:
// string address = "Beijing";
//private:
// string m_ID = "111111";
//};
//
//class BeiJingPerson:public person
//{
//public:
// void show()
// {
// cout << name<<address;
// }
//
//};
//
//
//class TianJingPerson :protected person
//{
//public:
// void show()
// {
// cout << name <<address;
// }
//
//};
//class HeNanJingPerson :private person
//{
//public:
// void show()
// {
// cout << name << address;
// }
//
//};
//
//class mm : public TianJingPerson
//{
//public:
// void show()
// {
// cout << name << address;
// }
//};
void main()
{
/*TianJingPerson tp; HeNanJingPerson hp; BeiJingPerson bp;
tp.show();
hp.show();
bp.name;*/
AudioPlayer* audioPlayer = new AudioPlayer();
audioPlayer->play("mp3", "beyond the horizon.mp3");
audioPlayer->play("mp4", "alone.mp4");
audioPlayer->play("vlc", "far far away.vlc");
audioPlayer->play("avi", "mind me.avi");
}