Cocos2d-x设计模式 -- 外观模式
来源:互联网 发布:msqrd相似的软件 编辑:程序博客网 时间:2024/05/17 07:55
仍然先来说明一下何为外观模式,一个复杂的系统包含很多子系统,为了使用这个复杂的系统,我们定义一个统一的接口来使用这个复杂的系统。当用户操作的时候只要调用我们提供的这个接口就好了,至于底层的这个复杂的系统,用户不必关系是如何工作的。这里列举一个网上的例子,编译系统是一个复杂的系统,包括什么词法分析,语法分析,语义分析等等,用户在编译程序的时候不需要了解这个复杂的系统到底是怎么编译的,只要使用统一的接口编译就可以了,代码如下:
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;
}
};
//Facade
class
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来实现这个功能,这就是所谓的灵活性吧。
0 0
- Cocos2D-X设计模式:外观模式
- Cocos2d-x设计模式 -- 外观模式
- Cocos2D-X设计模式:外观模式
- Cocos2d-x 设计模式 (4):外观模式
- Cocos2d-x设计模式之四 :外观模式
- Cocos2d-x设计模式发掘之四:外观模式
- Cocos2d-x设计模式发掘之四:外观模式
- cocos2d-x设计模式发掘之四:外观模式
- Cocos2d-x的设计模式
- 设计模式--外观模式
- 设计模式-外观模式
- 设计模式:外观模式
- 设计模式----外观模式
- 设计模式------外观模式
- 设计模式 外观模式
- 设计模式-【外观模式】
- 设计模式-----外观模式
- 设计模式 - 外观模式
- 我眼里的正则表达式(入门)
- Makefile文件生成
- 利用拼接字符串的方式来写一个简化版的后台网站(推荐初学者进)
- error LNK1169: 找到一个或多个多重定义的符号D:\Visual Studio 2013\...\Debug\.exe11
- BZOJ1934【最小割】
- Cocos2d-x设计模式 -- 外观模式
- JeeWx 微信开发公开课(Jeewx-API 专题),今晚8点不见不散
- "undefined reference to" 问题解决方法
- 手势识别(一)手势识别器
- xcode 6.1解析外部的ips 崩溃日志
- ExtJS获取父子、兄弟容器元素方法
- 随记(电脑硬件对比)
- ajax回调与asynctask 下载比较
- Android Fragments Example