Bridge设计模式

一篇不错的文章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关系!



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值