桥接模式:将抽象部分与它的实现部分分离,使它们都可以独立地变化。 实现系统可能有多角度分类,每一种分类都有可能变化,那么就把这种多角度分离出来,让它们独立变化,减少它们之间的耦合。
#include <iostream>
using namespace std;
class HandsetSoft
{
public:
virtual void Run() = 0;
virtual ~HandsetSoft(){}
};
class HandsetGame : public HandsetSoft
{
public:
void Run()
{
cout<<"运行手机游戏"<<endl;
}
~HandsetGame()
{}
};
class HandsetAddressList : public HandsetSoft
{
public:
void Run()
{
cout<<"运行手机通讯录"<<endl;
}
~HandsetAddressList()
{}
};
class HandsetBrand
{
protected:
HandsetSoft* soft;
public:
void SetHandsetSoft(HandsetSoft* soft)
{
this->soft = soft;
}
virtual void Run() = 0;
virtual ~HandsetBrand() {}
};
class HandsetBrandN : public HandsetBrand
{
public:
void Run()
{
soft->Run();
}
~HandsetBrandN() {}
};
class HandsetBrandM : public HandsetBrand
{
public:
void Run()
{
soft->Run();
}
~HandsetBrandM() {}
};
int main()
{
HandsetBrand* ab;
HandsetBrandN hbn;
HandsetGame hg;
ab = &hbn;
ab->SetHandsetSoft(&hg);
ab->Run();
HandsetAddressList hl;
ab->SetHandsetSoft(&hl);
ab->Run();
HandsetBrandM hbm;
ab = &hbm;
ab->SetHandsetSoft(&hg);
ab->Run();
ab->SetHandsetSoft(&hl);
ab->Run();
return 0;
}