会员
周边
众包
新闻
博问
闪存
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
乾坤塾
理想编程
博客园
首页
新随笔
联系
管理
订阅
Bridge桥接模式2 C++理解的第二个版本
Bridge桥接模式3 C++理解的第三个版本 代码更加专业了 将定义与实现分开,分别放到.h和.cpp中;客户端调用也单独实现在一个cpp文件中。
//
Bridge桥接模式2 C++理解的第二个版本
//
上一篇是根据我用Delphi实现的Bridge的一个简单改写,
//
在网上又看了一些实现,现加入了类的构造与析构函数,
//
一边学习设计模式,一边恢复对C++的记忆,不断掌握C++
//
下一步将声明与实现分开。
#include
<
iostream
>
using
namespace
std;
class
Implementor
{
public
:
virtual
void
OperationImp()
=
0
;
virtual
~
Implementor()
{}
;
protected
:
Implementor()
{}
;
}
;
class
ConcreteImplementorA:
public
Implementor
{
public
:
void
OperationImp()
{
cout
<<
"
ConcreteImplementorA.OperationImp
"
<<
endl;
}
ConcreteImplementorA()
{}
;
virtual
~
ConcreteImplementorA()
{}
;
}
;
class
ConcreteImplementorB:
public
Implementor
{
public
:
void
OperationImp()
{
cout
<<
"
ConcreteImplementorB.OperationImp
"
<<
endl;
}
ConcreteImplementorB()
{}
;
virtual
~
ConcreteImplementorB()
{}
;
}
;
class
Abstraction
{
protected
:
Implementor
*
pImp;
Abstraction()
{}
;
public
:
virtual
void
Operation()
{
if
(pImp)
pImp
->
OperationImp();
}
void
SetImplementor(Implementor
*
p)
{
pImp
=
p;
}
virtual
~
Abstraction()
{}
;
}
;
class
RefinedAbstraction:
public
Abstraction
{
public
:
void
Operation()
{
Abstraction::Operation();
//
类似Delphi中的Inherited;
cout
<<
"
Do some other thing
"
<<
endl;
}
RefinedAbstraction()
{}
;
virtual
~
RefinedAbstraction()
{}
;
}
;
int
main(
int
argc,
char
*
argv[])
{
cout
<<
"
hello, 这个一个桥接模式的演示:
"
<<
endl;
//
RefinedAbstraction* pR = new RefinedAbstraction();
Abstraction
*
pAbstraction
=
new
RefinedAbstraction();
Implementor
*
pImplementor;
pImplementor
=
new
ConcreteImplementorA();
pAbstraction
->
SetImplementor(pImplementor);
pAbstraction
->
Operation();
delete pImplementor;
pImplementor
=
new
ConcreteImplementorB();
pAbstraction
->
SetImplementor(pImplementor);
pAbstraction
->
Operation();
delete pImplementor;
return
0
;
}
posted @
2008-06-05 16:12
treemon
阅读(
801
) 评论(
0
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部
公告