适配器模式主要是将一个类接口转换成客户期待的另一个接口。
class VGA
{
public:
void play()
{
cout << "VGA" << endl;
}
};
class HDMI
{
public:
void play() {
cout << "HDMI" << endl;
}
};
class computer
{
public:
void playVideo(VGA* a)
{
a->play();
}
};
class VGATOHDMI:public VGA
{
HDMI* hdm;
public:
VGATOHDMI(HDMI* hdmi)
{
hdm = hdmi;
}
//此play函数相当于转换头
void play()
{
hdm->play();
}
};
int main()
{
computer* c = new computer();
c->playVideo(new VGA());
HDMI* hdi = new HDMI(); //本来不具有使用HDMI的功能,我们进行转换让其可以使用HDMI
c->playVideo(new VGATOHDMI(hdi));
}