设计模式中结构型模式(二)桥接模式(Bridge)

图示:

设计模式中结构型模式(二)桥接模式(Bridge)

模式Bridge的结构与对象适配器类似,但是Bridge模式的出发点不同:Bridge目的是将接口
部分和实现部分分离,从而对它们可以较为容易也相对独立的加以改变。而Adapter则意味着
改变一个已有对象的接口。
以下一些情况使用Bridge模式:
1、你不希望在抽象和它的实现部分之间有一个固定的绑定关系。例如这种情况可能是因为,在
程序运行时刻实现部分应可以被选择或者切换。
2、类的抽象以及它的实现都应该可以通过生成子类的方法加以扩充。这时Bridge模式使你可以对不同的抽象
接口和实现部分进行组合,并分别对它们进行扩充。
3、对一个抽象的实现部分的修改应对客户不产生影响,即客户的代码不必重新编译。
4、C++中你想对客户完全隐藏抽象的实现部分。在C++中,类的表示在类接口中是可见的。

代码Application类与IconWindow为外部接口类,它们从BaseWindow继承,都有接口方法DrawContents(),但完成的任务不同,这里的不同体现在两点,一点是作为接口函数,可以实现不同的接口功能,另一点是可以调用不同的实现类来完成所要定制的功能。
从代码中看出,通过字符串的切换便可达到调用不同的实现类,完成不同的功能,而实现类无需任何的改变。同样,实现类的改变也同样不会影响这里的接口部分。
原书中(指的那本《设计模式》)所述用一个factory工厂类来完成类的定义,将自动找寻到子类并new出来这样的功能封装在factory中,可惜我按书中的方法始终没成功,因此稍做了一些变通,或许使得实现类与接口类有一点的耦合,以后有时间再仔细研究一下。

以下为示例代码

设计模式中结构型模式(二)桥接模式(Bridge)//Bridge.h:interfacefortheBridgeclass.
设计模式中结构型模式(二)桥接模式(Bridge)
//
设计模式中结构型模式(二)桥接模式(Bridge)设计模式中结构型模式(二)桥接模式(Bridge)
/**//**//**///////////////////////////////////////////////////////////////////////
设计模式中结构型模式(二)桥接模式(Bridge)
设计模式中结构型模式(二)桥接模式(Bridge)
#if!defined(AFX_BRIDGE_H__C037F529_E449_4786_8DEE_D8293BF666D5__INCLUDED_)
设计模式中结构型模式(二)桥接模式(Bridge)
#defineAFX_BRIDGE_H__C037F529_E449_4786_8DEE_D8293BF666D5__INCLUDED_
设计模式中结构型模式(二)桥接模式(Bridge)
设计模式中结构型模式(二)桥接模式(Bridge)
#if_MSC_VER>1000
设计模式中结构型模式(二)桥接模式(Bridge)
#pragmaonce
设计模式中结构型模式(二)桥接模式(Bridge)
#endif//_MSC_VER>1000
设计模式中结构型模式(二)桥接模式(Bridge)
设计模式中结构型模式(二)桥接模式(Bridge)#include
"BasicClass.h"
设计模式中结构型模式(二)桥接模式(Bridge)
constMAX_PATH=250;
设计模式中结构型模式(二)桥接模式(Bridge)
classView;
设计模式中结构型模式(二)桥接模式(Bridge)
classWindowImp;
设计模式中结构型模式(二)桥接模式(Bridge)
设计模式中结构型模式(二)桥接模式(Bridge)
//Window的操作由WindowImp的接口定义。
设计模式中结构型模式(二)桥接模式(Bridge)
//那么一个窗口怎样得到正确的WindowI子类的实例呢?在本例我们假设Window类具有
设计模式中结构型模式(二)桥接模式(Bridge)
//职责,它的GetWindowImp操作负责从一个抽象工厂得到正确的实例,这个抽象工厂封
设计模式中结构型模式(二)桥接模式(Bridge)
//装了所有窗口系统的细节。
设计模式中结构型模式(二)桥接模式(Bridge)
classBaseWindow
设计模式中结构型模式(二)桥接模式(Bridge)设计模式中结构型模式(二)桥接模式(Bridge)
...{
设计模式中结构型模式(二)桥接模式(Bridge)
public:
设计模式中结构型模式(二)桥接模式(Bridge)设计模式中结构型模式(二)桥接模式(Bridge)BaseWindow()
...{_imp=0;}
设计模式中结构型模式(二)桥接模式(Bridge)
voidSetClassName(constchar*classname);
设计模式中结构型模式(二)桥接模式(Bridge)
constchar*GetClassNameA();
设计模式中结构型模式(二)桥接模式(Bridge)
设计模式中结构型模式(二)桥接模式(Bridge)
设计模式中结构型模式(二)桥接模式(Bridge)
//requestshandledbywindow
设计模式中结构型模式(二)桥接模式(Bridge)设计模式中结构型模式(二)桥接模式(Bridge)
virtualvoidDrawContents()...{};
设计模式中结构型模式(二)桥接模式(Bridge)
设计模式中结构型模式(二)桥接模式(Bridge)设计模式中结构型模式(二)桥接模式(Bridge)
virtualvoidOpen()...{}
设计模式中结构型模式(二)桥接模式(Bridge)
protected:
设计模式中结构型模式(二)桥接模式(Bridge)WindowImp
*GetWindowImp();
设计模式中结构型模式(二)桥接模式(Bridge)
private:
设计模式中结构型模式(二)桥接模式(Bridge)
char_className[MAX_PATH];
设计模式中结构型模式(二)桥接模式(Bridge)WindowImp
*_imp;
设计模式中结构型模式(二)桥接模式(Bridge)}
;
设计模式中结构型模式(二)桥接模式(Bridge)
设计模式中结构型模式(二)桥接模式(Bridge)
设计模式中结构型模式(二)桥接模式(Bridge)
//Window维护一个对WindowImp的引用,WindowImp抽象类定义了一个对底层窗口系统的接口
设计模式中结构型模式(二)桥接模式(Bridge)
classWindowImp
设计模式中结构型模式(二)桥接模式(Bridge)设计模式中结构型模式(二)桥接模式(Bridge)
...{
设计模式中结构型模式(二)桥接模式(Bridge)
public:
设计模式中结构型模式(二)桥接模式(Bridge)
virtualvoidDeviceRect(Coord,Coord,Coord,Coord)=0;
设计模式中结构型模式(二)桥接模式(Bridge)
virtualvoidDeviceText(constchar*,Coord,Coord)=0;
设计模式中结构型模式(二)桥接模式(Bridge)
virtualvoidDeviceBitmap(constchar*,Coord,Coord)=0;
设计模式中结构型模式(二)桥接模式(Bridge)
//lotsmorefunctionsfordrawingonwindows
设计模式中结构型模式(二)桥接模式(Bridge)
protected:
设计模式中结构型模式(二)桥接模式(Bridge)设计模式中结构型模式(二)桥接模式(Bridge)WindowImp()
...{}
设计模式中结构型模式(二)桥接模式(Bridge)}
;
设计模式中结构型模式(二)桥接模式(Bridge)
设计模式中结构型模式(二)桥接模式(Bridge)
//Window的子类定义了应用程序可能用到的不同类型蝗窗口,如应用窗口,图标,对话框临时
设计模式中结构型模式(二)桥接模式(Bridge)
//窗口以及工具箱的移动面板等。
设计模式中结构型模式(二)桥接模式(Bridge)
//例如ApplicationWindow类将实现DrawContents操作以绘制它所存储的View实例:
设计模式中结构型模式(二)桥接模式(Bridge)
classApplicationWindow:publicBaseWindow
设计模式中结构型模式(二)桥接模式(Bridge)设计模式中结构型模式(二)桥接模式(Bridge)
...{
设计模式中结构型模式(二)桥接模式(Bridge)
public:
设计模式中结构型模式(二)桥接模式(Bridge)
//
设计模式中结构型模式(二)桥接模式(Bridge)
virtualvoidDrawContents();
设计模式中结构型模式(二)桥接模式(Bridge)}
;
设计模式中结构型模式(二)桥接模式(Bridge)
设计模式中结构型模式(二)桥接模式(Bridge)
设计模式中结构型模式(二)桥接模式(Bridge)
//IconWindow中存储了它所显示的图标对应的位图名
设计模式中结构型模式(二)桥接模式(Bridge)
//并且实现DrawContents操作将这个位图绘制在窗口上。
设计模式中结构型模式(二)桥接模式(Bridge)
classIconWindow:publicBaseWindow
设计模式中结构型模式(二)桥接模式(Bridge)设计模式中结构型模式(二)桥接模式(Bridge)
...{
设计模式中结构型模式(二)桥接模式(Bridge)
public:
设计模式中结构型模式(二)桥接模式(Bridge)
//
设计模式中结构型模式(二)桥接模式(Bridge)
virtualvoidDrawContents();
设计模式中结构型模式(二)桥接模式(Bridge)
private:
设计模式中结构型模式(二)桥接模式(Bridge)
constchar*_bitmapName;
设计模式中结构型模式(二)桥接模式(Bridge)}
;
设计模式中结构型模式(二)桥接模式(Bridge)
设计模式中结构型模式(二)桥接模式(Bridge)
设计模式中结构型模式(二)桥接模式(Bridge)
//具体的WindowImp子类可支持不同的窗口系统, XWindowImp子类支持XWindow窗口系统:
设计模式中结构型模式(二)桥接模式(Bridge)
classXWindowImp:publicWindowImp
设计模式中结构型模式(二)桥接模式(Bridge)设计模式中结构型模式(二)桥接模式(Bridge)
...{
设计模式中结构型模式(二)桥接模式(Bridge)
public:
设计模式中结构型模式(二)桥接模式(Bridge)设计模式中结构型模式(二)桥接模式(Bridge)XWindowImp()
...{}
设计模式中结构型模式(二)桥接模式(Bridge)
设计模式中结构型模式(二)桥接模式(Bridge)
virtualvoidDeviceRect(Coord,Coord,Coord,Coord);
设计模式中结构型模式(二)桥接模式(Bridge)
virtualvoidDeviceText(constchar*,Coord,Coord);
设计模式中结构型模式(二)桥接模式(Bridge)
virtualvoidDeviceBitmap(constchar*,Coord,Coord);
设计模式中结构型模式(二)桥接模式(Bridge)
//remainderofpublicinterface
设计模式中结构型模式(二)桥接模式(Bridge)
private:
设计模式中结构型模式(二)桥接模式(Bridge)
//lotsofXWindowsystem-specificstate,including:
设计模式中结构型模式(二)桥接模式(Bridge)
//Display*_dpy;
设计模式中结构型模式(二)桥接模式(Bridge)
//Drawable_winid;//windowid;
设计模式中结构型模式(二)桥接模式(Bridge)
//GC_gc;//windowgraphiccontext
设计模式中结构型模式(二)桥接模式(Bridge)
}
;
设计模式中结构型模式(二)桥接模式(Bridge)
设计模式中结构型模式(二)桥接模式(Bridge)
//对于PresentationManager(PM),
设计模式中结构型模式(二)桥接模式(Bridge)
//我们定义PMWindowImp类
设计模式中结构型模式(二)桥接模式(Bridge)
classPMWindowImp:publicWindowImp
设计模式中结构型模式(二)桥接模式(Bridge)设计模式中结构型模式(二)桥接模式(Bridge)
...{
设计模式中结构型模式(二)桥接模式(Bridge)
public:
设计模式中结构型模式(二)桥接模式(Bridge)设计模式中结构型模式(二)桥接模式(Bridge)PMWindowImp()
...{}
设计模式中结构型模式(二)桥接模式(Bridge)
virtualvoidDeviceRect(Coord,Coord,Coord,Coord);
设计模式中结构型模式(二)桥接模式(Bridge)
virtualvoidDeviceText(constchar*,Coord,Coord);
设计模式中结构型模式(二)桥接模式(Bridge)
virtualvoidDeviceBitmap(constchar*,Coord,Coord);
设计模式中结构型模式(二)桥接模式(Bridge)
设计模式中结构型模式(二)桥接模式(Bridge)
//remainderofpublicinterface
设计模式中结构型模式(二)桥接模式(Bridge)
private:
设计模式中结构型模式(二)桥接模式(Bridge)
//lotsofPMwindowsystem-specificatate,including
设计模式中结构型模式(二)桥接模式(Bridge)
//HPS_hps;
设计模式中结构型模式(二)桥接模式(Bridge)
}
;
设计模式中结构型模式(二)桥接模式(Bridge)
设计模式中结构型模式(二)桥接模式(Bridge)
设计模式中结构型模式(二)桥接模式(Bridge)
classWindowSystemFactory
设计模式中结构型模式(二)桥接模式(Bridge)设计模式中结构型模式(二)桥接模式(Bridge)
...{
设计模式中结构型模式(二)桥接模式(Bridge)
private:
设计模式中结构型模式(二)桥接模式(Bridge)
staticWindowSystemFactory*_instance;
设计模式中结构型模式(二)桥接模式(Bridge)
public:
设计模式中结构型模式(二)桥接模式(Bridge)
staticWindowSystemFactory*Instance();
设计模式中结构型模式(二)桥接模式(Bridge)WindowImp
*MakeWindowImp(constchar*className);
设计模式中结构型模式(二)桥接模式(Bridge)}
;
设计模式中结构型模式(二)桥接模式(Bridge)
#endif//!defined(AFX_BRIDGE_H__C037F529_E449_4786_8DEE_D8293BF666D5__INCLUDED_)
设计模式中结构型模式(二)桥接模式(Bridge)
设计模式中结构型模式(二)桥接模式(Bridge)
设计模式中结构型模式(二)桥接模式(Bridge)
设计模式中结构型模式(二)桥接模式(Bridge)
设计模式中结构型模式(二)桥接模式(Bridge)
设计模式中结构型模式(二)桥接模式(Bridge)
设计模式中结构型模式(二)桥接模式(Bridge)
//Bridge.cpp:implementationoftheBridgeclass.
设计模式中结构型模式(二)桥接模式(Bridge)
//
设计模式中结构型模式(二)桥接模式(Bridge)设计模式中结构型模式(二)桥接模式(Bridge)
/**//**//**///////////////////////////////////////////////////////////////////////
设计模式中结构型模式(二)桥接模式(Bridge)
设计模式中结构型模式(二)桥接模式(Bridge)#include
"stdafx.h"
设计模式中结构型模式(二)桥接模式(Bridge)#include
"Bridge.h"
设计模式中结构型模式(二)桥接模式(Bridge)
设计模式中结构型模式(二)桥接模式(Bridge)#include
<string.h>
设计模式中结构型模式(二)桥接模式(Bridge)#include
<tchar.h>
设计模式中结构型模式(二)桥接模式(Bridge)#include
<wtypes.h>
设计模式中结构型模式(二)桥接模式(Bridge)
设计模式中结构型模式(二)桥接模式(Bridge)#include
<string>
设计模式中结构型模式(二)桥接模式(Bridge)
usingstd::string;
设计模式中结构型模式(二)桥接模式(Bridge)
设计模式中结构型模式(二)桥接模式(Bridge)
#definemax(a,b)(((a)>(b))?(a):(b))
设计模式中结构型模式(二)桥接模式(Bridge)
#definemin(a,b)(((a)<(b))?(a):(b))
设计模式中结构型模式(二)桥接模式(Bridge)
#defineabs(a)((a)<0)?(-(a)):(a)
设计模式中结构型模式(二)桥接模式(Bridge)设计模式中结构型模式(二)桥接模式(Bridge)
/**//**//**///////////////////////////////////////////////////////////////////////
设计模式中结构型模式(二)桥接模式(Bridge)//Construction/Destruction
设计模式中结构型模式(二)桥接模式(Bridge)设计模式中结构型模式(二)桥接模式(Bridge)
/**//**//**///////////////////////////////////////////////////////////////////////
设计模式中结构型模式(二)桥接模式(Bridge)
设计模式中结构型模式(二)桥接模式(Bridge)
//////////////////////////////////////////////////////////////////////

设计模式中结构型模式(二)桥接模式(Bridge)//Construction/Destruction
设计模式中结构型模式(二)桥接模式(Bridge)设计模式中结构型模式(二)桥接模式(Bridge)
/**//**//**///////////////////////////////////////////////////////////////////////
设计模式中结构型模式(二)桥接模式(Bridge)
设计模式中结构型模式(二)桥接模式(Bridge)
voidBaseWindow::SetClassName(constchar*classname)
设计模式中结构型模式(二)桥接模式(Bridge)设计模式中结构型模式(二)桥接模式(Bridge)
...{
设计模式中结构型模式(二)桥接模式(Bridge)strcpy(_className,classname);
设计模式中结构型模式(二)桥接模式(Bridge)}

设计模式中结构型模式(二)桥接模式(Bridge)
设计模式中结构型模式(二)桥接模式(Bridge)
constchar*BaseWindow::GetClassNameA()
设计模式中结构型模式(二)桥接模式(Bridge)设计模式中结构型模式(二)桥接模式(Bridge)
...{
设计模式中结构型模式(二)桥接模式(Bridge)
return_className;
设计模式中结构型模式(二)桥接模式(Bridge)}

设计模式中结构型模式(二)桥接模式(Bridge)
设计模式中结构型模式(二)桥接模式(Bridge)WindowImp
*BaseWindow::GetWindowImp()
设计模式中结构型模式(二)桥接模式(Bridge)设计模式中结构型模式(二)桥接模式(Bridge)
...{
设计模式中结构型模式(二)桥接模式(Bridge)
constchar*className=GetClassNameA();
设计模式中结构型模式(二)桥接模式(Bridge)
if(_imp==0)
设计模式中结构型模式(二)桥接模式(Bridge)设计模式中结构型模式(二)桥接模式(Bridge)
...{
设计模式中结构型模式(二)桥接模式(Bridge)_imp
=WindowSystemFactory::Instance()->MakeWindowImp(className);
设计模式中结构型模式(二)桥接模式(Bridge)}

设计模式中结构型模式(二)桥接模式(Bridge)
return_imp;
设计模式中结构型模式(二)桥接模式(Bridge)}

设计模式中结构型模式(二)桥接模式(Bridge)
设计模式中结构型模式(二)桥接模式(Bridge)
设计模式中结构型模式(二)桥接模式(Bridge)
设计模式中结构型模式(二)桥接模式(Bridge)
设计模式中结构型模式(二)桥接模式(Bridge)
voidApplicationWindow::DrawContents()
设计模式中结构型模式(二)桥接模式(Bridge)设计模式中结构型模式(二)桥接模式(Bridge)
...{
设计模式中结构型模式(二)桥接模式(Bridge)SetClassName(_T(
"XWindowImp"));
设计模式中结构型模式(二)桥接模式(Bridge)
设计模式中结构型模式(二)桥接模式(Bridge)WindowImp
*imp=GetWindowImp();
设计模式中结构型模式(二)桥接模式(Bridge)
if(imp!=0)
设计模式中结构型模式(二)桥接模式(Bridge)设计模式中结构型模式(二)桥接模式(Bridge)
...{
设计模式中结构型模式(二)桥接模式(Bridge)imp
->DeviceRect(0,0,0,0);
设计模式中结构型模式(二)桥接模式(Bridge)}

设计模式中结构型模式(二)桥接模式(Bridge)
//GetView()->DrawOn(this);
设计模式中结构型模式(二)桥接模式(Bridge)
}

设计模式中结构型模式(二)桥接模式(Bridge)
设计模式中结构型模式(二)桥接模式(Bridge)
设计模式中结构型模式(二)桥接模式(Bridge)
voidIconWindow::DrawContents()
设计模式中结构型模式(二)桥接模式(Bridge)设计模式中结构型模式(二)桥接模式(Bridge)
...{
设计模式中结构型模式(二)桥接模式(Bridge)SetClassName(_T(
"PMWindowImp"));
设计模式中结构型模式(二)桥接模式(Bridge)WindowImp
*imp=GetWindowImp();
设计模式中结构型模式(二)桥接模式(Bridge)
if(imp!=0)
设计模式中结构型模式(二)桥接模式(Bridge)设计模式中结构型模式(二)桥接模式(Bridge)
...{
设计模式中结构型模式(二)桥接模式(Bridge)imp
->DeviceBitmap(_bitmapName,0.0,0.0);
设计模式中结构型模式(二)桥接模式(Bridge)}

设计模式中结构型模式(二)桥接模式(Bridge)}

设计模式中结构型模式(二)桥接模式(Bridge)
设计模式中结构型模式(二)桥接模式(Bridge)
voidXWindowImp::DeviceRect(Coordx0,Coordy0,Coordx1,Coordy1)
设计模式中结构型模式(二)桥接模式(Bridge)设计模式中结构型模式(二)桥接模式(Bridge)
...{
设计模式中结构型模式(二)桥接模式(Bridge)
#pragmawarning(disable:4244)
设计模式中结构型模式(二)桥接模式(Bridge)
intx=min(x0,x1);
设计模式中结构型模式(二)桥接模式(Bridge)
inty=min(y0,y1);
设计模式中结构型模式(二)桥接模式(Bridge)
intw=abs(x0-x1);
设计模式中结构型模式(二)桥接模式(Bridge)
inth=abs(y0-y1);
设计模式中结构型模式(二)桥接模式(Bridge)printf(
"XWindowImp::DeviceRect ");
设计模式中结构型模式(二)桥接模式(Bridge)
设计模式中结构型模式(二)桥接模式(Bridge)
#pragmawarning(disable:4244)
设计模式中结构型模式(二)桥接模式(Bridge)
//XDrawRectangele(_dpy,_winid,_gc,x,y,w,h);
设计模式中结构型模式(二)桥接模式(Bridge)
}

设计模式中结构型模式(二)桥接模式(Bridge)
设计模式中结构型模式(二)桥接模式(Bridge)
voidXWindowImp::DeviceText(constchar*,Coord,Coord)
设计模式中结构型模式(二)桥接模式(Bridge)设计模式中结构型模式(二)桥接模式(Bridge)
...{
设计模式中结构型模式(二)桥接模式(Bridge)printf(
"XWindowImp::Devicetext ");
设计模式中结构型模式(二)桥接模式(Bridge)}

设计模式中结构型模式(二)桥接模式(Bridge)
voidXWindowImp::DeviceBitmap(constchar*,Coord,Coord)
设计模式中结构型模式(二)桥接模式(Bridge)设计模式中结构型模式(二)桥接模式(Bridge)
...{
设计模式中结构型模式(二)桥接模式(Bridge)printf(
"XWindowImp::DeviceBitmap ");
设计模式中结构型模式(二)桥接模式(Bridge)