仍然先来说明一下何为外观模式,一个复杂的系统包含很多子系统,为了使用这个复杂的系统,我们定义一个统一的接口来使用这个复杂的系统。当用户操作的时候只要调用我们提供的这个接口就好了,至于底层的这个复杂的系统,用户不必关系是如何工作的。这里列举一个网上的例子,编译系统是一个复杂的系统,包括什么词法分析,语法分析,语义分析等等,用户在编译程序的时候不需要了解这个复杂的系统到底是怎么编译的,只要使用统一的接口编译就可以了,代码如下:
|
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
|
#include <string>using namespace std;
//代码扫描读入class CCodeScanner
{public:
void Scan()
{
cout << "scan code" << endl;
}
};//语法分析class CCodeParser
{public:
void Parse()
{
cout << "parse code" << endl;
}
};//代码生成class CCodeGenerator
{public:
void Generate()
{
cout << "generate code" << endl;
}
};//Facadeclass CCompiler
{public:
//更高层的统一接口
void Compile()
{
cout << "compile program…" << endl;
CCodeScanner scanner;
scanner.Scan();
CCodeParser parser;
parser.Parse();
CCodeGenerator generator;
generator.Generate();
}
};int main()
{ CCompiler compiler;
compiler.Compile();
return 0;
} |
一个相似的例子。
|
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
|
class CPU {
public void freeze() { ... }
public void jump(long position) { ... }
public void execute() { ... }
}class Memory {
public void load(long position, byte[] data) { ... }
}class HardDrive {
public byte[] read(long lba, int size) { ... }
}/* Facade */class Computer {
private CPU cpu;
private Memory memory;
private HardDrive hardDrive;
public Computer() {
this.cpu = new CPU();
this.memory = new Memory();
this.hardDrive = new HardDrive();
}
public void startComputer() {
cpu.freeze();
memory.load(BOOT_ADDRESS, hardDrive.read(BOOT_SECTOR, SECTOR_SIZE));
cpu.jump(BOOT_ADDRESS);
cpu.execute();
}
}/* Client */class You {
public static void main(String[] args) {
Computer facade = new Computer();
facade.startComputer();
}
} |
从上边的例子中可以看出来,这个外观模式不是封装的一个对象,而是封装了一组对象,我们通过它的接口来实现功能,而内部它调用了各个对象的功能模块。这样做的好处不用说也明白了吧,就是简化了用户的操作,当然用户如果想要调用底层的代码也是可以的,这样可以获得更灵活的功能。在cocos2dx中一个外观模式的例子就是SimpleAudioEngine,我们播放声音就是调用这个类的接口来完成的,而底层它是使用了CDSoundEngine、CDAudioManage这俩个类的相关管理声音的函数。我们知道SimpleAudioEngine是没有函数提供给我们循环播放音效的,但是我们可以使用底层的CDAudioManage来实现这个功能,这就是所谓的灵活性吧。
来源网址:http://www.zaojiahua.com/skin-mode.html