Bridge模式将类的定义与实现彻底解耦,采用组合的方式进行处理。
代码如下:
1
#include <iostream>
2
#include <stdio.h>
3
#include <stdlib.h>
4
#include <stdarg.h>
5
6
#define STRLEN 100
7
class CImplement
8

{
9
protected:
10
CImplement()
11
{
12
13
}
14
public:
15
~CImplement()
16
{
17
18
}
19
20
virtual void Operatorion() = 0;
21
virtual void PrintMsg(char * format,
) = 0;
22
23
};
24
25
26
class CExtendImp:public CImplement
27

{
28
public:
29
CExtendImp()
30
{
31
32
}
33
34
~CExtendImp()
35
{
36
37
}
38
39
void Operatorion()
40
{
41
PrintMsg("%s\t %s","Hello" ,"World!");
42
}
43
44
void PrintMsg(char * format,
)
45
{
46
va_list tmp_list;
47
va_start(tmp_list,format);
48
char* c_buffer = new char[STRLEN];
49
vsprintf(c_buffer,format,tmp_list);
50
fprintf(stderr,"\r\n*********\r\n%s\r\n",c_buffer);
51
}
52
};
53
54
class CAbstract
55

{
56
protected:
57
CAbstract()
58
{
59
60
}
61
public:
62
~CAbstract()
63
{
64
65
}
66
67
virtual void operation() = 0;
68
};
69
70
class CExtendAbstract
71

{
72
public:
73
CExtendAbstract(CImplement * data)
74
{
75
_imp = NULL;
76
_imp = data;
77
}
78
~CExtendAbstract()
79
{
80
81
}
82
83
void operation()
84
{
85
_imp->Operatorion();
86
}
87
protected:
88
CImplement * _imp;
89
};
90
91
int main(int argc ,char * argv[])
92

{
93
CImplement * value = new CExtendImp();
94
CExtendAbstract* abstr = new CExtendAbstract(value);
95
abstr->operation();
96
}
posted on 2009-06-18 17:59
Super- 阅读(1120)
评论(2) 编辑 收藏 引用