设计模式之Bridge桥接模式

1. 实现类

1.1. Implementor(实现类接口)

//Implementor(实现类接口)
class Color
{
public:
	 virtual ~Color() = default;
	 virtual void fillColor() = 0;

private:

};

1.2. ConcreteImplementor(具体实现类),Red,Green,Blue

//ConcreteImplementor(具体实现类),Red,Green,Blue
class RedColor : public Color
{
public:
	RedColor()
	{
		std::cout << "RedColor 构造函数" << std::endl;
		m_sColorType = "Red";
	}
	~RedColor()
	{
		std::cout << "~RedColor 析构函数" << std::endl;
	}

	void fillColor() override
	{
		std::cout << "填充颜色;" << m_sColorType << std::endl;
	}

private:
	std::string m_sColorType;
};

class GreenColor : public Color
{
public:
	GreenColor()
	{
		std::cout << "GreenColor 构造函数" << std::endl;
		m_sColorType = "Green";
	}
	~GreenColor()
	{
		std::cout << "~GreenColor 析构函数" << std::endl;
	}

	void fillColor() override
	{
		std::cout << "填充颜色;" << m_sColorType << std::endl;
	}

private:
	std::string m_sColorType;
};

class BlueColor : public Color
{
public:
	BlueColor()
	{
		std::cout << "BlueColor 构造函数" << std::endl;
		m_sColorType = "Blue";
	}
	~BlueColor()
	{
		std::cout << "~BlueColor 析构函数" << std::endl;
	}

	void fillColor() override
	{
		std::cout << "填充颜色;" << m_sColorType << std::endl;
	}

private:
	std::string m_sColorType;
};

2. 抽象类

2.1. Abstraction(抽象类)

//Abstraction(抽象类)
class Shape
{
public:
	virtual ~Shape() = default;
	virtual void showShape() = 0;
	virtual void chooseColor(Color* col) = 0;
protected:
	virtual void drawShape() = 0;
	virtual void drawColor() = 0;

	Color* m_pColor = nullptr;
};

2.2. RefinedAbstraction(扩充抽象类)Circle, Rectangle

//RefinedAbstraction(扩充抽象类)Circle, Rectangle,
class CircleShape : public Shape
{
public:
	CircleShape()
	{
		std::cout << "CircleShape 构造函数" << std::endl;
		m_sShapeType = "Circle";
	}
	~CircleShape()
	{
		std::cout << "~CircleShape 析构函数" << std::endl;

		if (nullptr != m_pColor)
		{
			delete m_pColor;
			m_pColor = nullptr;
		}
	}

	void showShape() override
	{
		drawShape();
		drawColor();
	}

	void chooseColor(Color* col) override
	{
		m_pColor = col;
	}

private:
	void drawShape() override
	{
		std::cout << "绘制图形:" << m_sShapeType << std::endl;
	}

	void drawColor() override
	{
		m_pColor->fillColor();
	}

	std::string m_sShapeType;
};

class RectangleShape : public Shape
{
public:
	RectangleShape()
	{
		std::cout << "CircleShape 构造函数" << std::endl;
		m_sShapeType = "Rectangle";
	}
	~RectangleShape()
	{
		std::cout << "~CircleShape 析构函数" << std::endl;

		if (nullptr != m_pColor)
		{
			delete m_pColor;
			m_pColor = nullptr;
		}
	}

	void showShape() override
	{
		drawShape();
		drawColor();
	}

	void chooseColor(Color* col) override
	{
		m_pColor = col;
	}

private:
	void drawShape() override
	{
		std::cout << "绘制图形:" << m_sShapeType << std::endl;
	}

	void drawColor() override
	{
		m_pColor->fillColor();
	}

	std::string m_sShapeType;
};

3. 主函数


#include <list>
#include <string>
#include <iostream> 
using namespace std;

void main()
{
	Shape *shape;
	//绘制 红色 圆形
	std::cout << "=====绘制 红色 圆形=====" << std::endl;
	shape = new CircleShape();
	shape->chooseColor(new RedColor());
	shape->showShape();
	delete shape;

	//绘制 绿色 圆形
	std::cout << "=====绘制 绿色 圆形=====" << std::endl;
	shape = new CircleShape();
	shape->chooseColor(new GreenColor());
	shape->showShape();
	delete shape;

	//绘制 蓝色 矩形
	std::cout << "=====绘制 蓝色 矩形=====" << std::endl;
	shape = new RectangleShape();
	shape->chooseColor(new BlueColor());
	shape->showShape();
	delete shape;

	system("pause");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小胖七少爷

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值