桥接模式
比如一个对象,如果A维度有3种变化,B维度有4种变化,C维度有2中变化。直接使用继承的方式,会有3x4x2种可能,即出现24个类,而且每个类都包含A B C维度,职责不单一,扩展性很差。
使用桥接模式,通过组合的方式,那么只有3+4+2=9个类,同时每个类的职责都单一。扩展性也很强。
代码如下:
#include<iostream>
#include<string>
using namespace std;
class Paint//喷漆
{
public:
virtual void Do() = 0;
};
class WhitePaint : public Paint //白色喷漆
{
public:
virtual void Do()
{
printf("喷白色的漆\r\n");
}
};
class BluePaint : public Paint //蓝色喷漆
{
public:
virtual void Do()
{
printf("喷蓝色的漆\r\n");
}
};
class BlackPaint : public Paint //黑色喷漆
{
public:
virtual void Do()
{
printf("喷黑色的漆\r\n");
}
};
class Car
{
public:
virtual void SetPaint(Paint* p)
{
m_Paint = p;
}
virtual Paint* GetPaint()
{
return m_Paint;
}
virtual void DoPaint() = 0; //喷漆
virtual void Run() = 0; //开车
protected:
Paint* m_Paint;
};
class SportsCar : public Car //跑车
{
public:
virtual void DoPaint()
{
printf("跑车喷漆: ");
m_Paint->Do();
}
virtual void Run()
{
printf("跑车跑起来,谁也追不上\r\n\r\n");
}
};
class Trunk : public Car //货车
{
public:
virtual void DoPaint()
{
printf("货车喷漆: ");
m_Paint->Do();
}
virtual void Run()
{
printf("货车跑的慢,但载东西最多\r\n\r\n");
}
};
class Sedan : public Car //轿车
{
public:
virtual void DoPaint()
{
printf("轿车喷漆: ");
m_Paint->Do();
}
virtual void Run()
{
printf("轿车最适合上班族代步使用\r\n\r\n");
}
};
void main()
{
Car* sport = new SportsCar;
Car* trunk = new Trunk;
Car* sedan = new Sedan;
Paint* white = new WhitePaint;
Paint* black = new BlackPaint;
Paint* blue = new BluePaint;
sport->SetPaint(white);
sport->DoPaint();
sport->Run();
trunk->SetPaint(black);
trunk->DoPaint();
trunk->Run();
sedan->SetPaint(blue);
sedan->DoPaint();
sedan->Run();
system("pause");
}
结果:
来源:CSDN
作者:HairLikeSnow
链接:https://blog.csdn.net/FairLikeSnow/article/details/103752815