posts - 11,comments - 13,trackbacks - 0
Bridge模式将类的定义与实现彻底解耦,采用组合的方式进行处理。
代码如下:
 1#include <iostream>
 2#include <stdio.h>
 3#include <stdlib.h>
 4#include <stdarg.h>
 5
 6#define STRLEN    100
 7class CImplement
 8{
 9protected:
10    CImplement()
11    {
12
13    }

14public:
15    ~CImplement()
16    {
17
18    }

19
20    virtual void Operatorion() = 0;
21    virtual void PrintMsg(char * format,= 0;
22    
23}
;
24
25
26class CExtendImp:public CImplement
27{
28public:
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
54class CAbstract
55{
56protected:
57    CAbstract()
58    {
59
60    }

61public:
62    ~CAbstract()
63    {
64
65    }

66
67    virtual void operation() = 0;
68}
;
69
70class CExtendAbstract
71{
72public:
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    }

87protected:
88    CImplement * _imp;
89}
;
90
91int 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- 阅读(1096) 评论(2)  编辑 收藏 引用

FeedBack:
# re: Bridge模式
2009-06-19 03:16 | 小不点
CExtendAbstract 是不是继承于CAbstract,要不然写CAbstract这个类偶看就没必要了吧,...  回复  更多评论
  
# re: Bridge模式
2009-06-19 09:57 | Super-
呵呵,对的哦,忘了写public继承了;
class CExtendAbstract:public CAbstract
另外将
CExtendAbstract* abstr = new CExtendAbstract(value);
修改为
CAbstract* abstr = new CExtendAbstract(value);
  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理