一篇不错的文章http://blog.csdn.net/hguisu/article/details/7529194
文章中以将毛笔大小和颜色分开实现为实例阐述了Bridge模式的优点。作者给出的是PHP实现代码,这里给出C++实现代码!
BrushPenAbstraction.h
#ifndef _BRUSHPENABSTRACTION_H_
#define _BRUSHPENABSTRACTION_H_
class ImplementorColor;
class BrushPenAbstraction
{
public:
BrushPenAbstraction();
virtual ~BrushPenAbstraction();
virtual void OperationDraw() = 0;
};
//SmallBrushPenAbstraction
class SmallBrushPenAbstraction :
public BrushPenAbstraction
{
public:
SmallBrushPenAbstraction(ImplementorColor * color = 0){ m_color = color; }
~SmallBrushPenAbstraction(){}
void OperationDraw();
private:
ImplementorColor * m_color;
};
//MidBrushPenAbstraction
class MidBrushPenAbstraction :
public BrushPenAbstraction
{
public:
MidBrushPenAbstraction(ImplementorColor * color = 0){ m_color = color; }
~MidBrushPenAbstraction(){}
void OperationDraw();
private:
ImplementorColor * m_color;
};
//BigBrushPenAbstraction
class BigBrushPenAbstraction :
public BrushPenAbstraction
{
public:
BigBrushPenAbstraction(ImplementorColor * color = 0);
~BigBrushPenAbstraction();
void OperationDraw();
private:
ImplementorColor * m_color;
};
#endif
BrushPenAbstraction.cpp
#include "BrushPenAbstraction.h"
#include "ImplementorColor.h"
#include <iostream>
//BrushPenAbstraction
BrushPenAbstraction::BrushPenAbstraction()
{
}
BrushPenAbstraction::~BrushPenAbstraction()
{
}
//SmallBrushPenAbstraction
void SmallBrushPenAbstraction::OperationDraw()
{
std::cout << "This is a small and ";
m_color->OperationDraw();
std::cout << " brush!\n";
}
//MidBrushPenAbstraction
void MidBrushPenAbstraction::OperationDraw()
{
std::cout << "This is a middle and ";
m_color->OperationDraw();
std::cout << " brush!\n";
}
//BigBrushPenAbstraction
BigBrushPenAbstraction::BigBrushPenAbstraction(ImplementorColor * color)
{
m_color = color;
}
BigBrushPenAbstraction::~BigBrushPenAbstraction()
{
}
void BigBrushPenAbstraction::OperationDraw()
{
std::cout << "This is a big and ";
m_color->OperationDraw();
std::cout << " brush!\n";
}
ImplementorColor.h
#ifndef _IMPLEMENTORCOLOR_H_
#define _IMPLEMENTORCOLOR_H_
class ImplementorColor
{
public:
ImplementorColor();
virtual ~ImplementorColor();
virtual void OperationDraw()=0;
};
//ImplementorRed
class ImplementorRed : public ImplementorColor
{
public:
ImplementorRed();
~ImplementorRed();
void OperationDraw();
};
//ImplementorGreen
class ImplementorGreen : public ImplementorColor
{
public:
ImplementorGreen();
~ImplementorGreen();
void OperationDraw();
};
//ImplementorBlue
class ImplementorBlue : public ImplementorColor
{
public:
ImplementorBlue();
~ImplementorBlue();
void OperationDraw();
};
#endif
ImplementorColor
#include <iostream>
#include "BrushPenAbstraction.h"
#include "ImplementorColor.h"
int main()
{
ImplementorColor * red = new ImplementorRed();
ImplementorColor * blue = new ImplementorBlue();
BrushPenAbstraction * bigBrush = new BigBrushPenAbstraction(red);
bigBrush->OperationDraw();
BrushPenAbstraction * midBrush = new MidBrushPenAbstraction(red);
midBrush->OperationDraw();
BrushPenAbstraction * smallBrush = new SmallBrushPenAbstraction(blue);
smallBrush->OperationDraw();
getchar();
return 0;
}
结果
C++ primer plus将继承描述为为一种 is-a 关系;而有元类是一种 has-a 关系,感觉Factory,Builder,包括这里的Bridge,可能还有更多我暂时没了解的设计模式,实现的也是一种has-a关系!