桥接模式之C++实现
#include <string>
#include <iostream>
using namespace std;
class Software
{
public:
virtual void Run() = 0;
};
class SoftwareA : public Software
{
public:
void Run()
{
cout << "运行SoftwareA" << endl;
}
};
class SoftwareB : public Software
{
public:
void Run()
{
cout << "运行SoftwareB" << endl;
}
};
class PlatForm
{
public:
virtual void SetSoftware(Software *) = 0;
virtual void Run() = 0;
};
class Computer : public PlatForm
{
private:
Software *pSoftware;
public:
void SetSoftware(Software *soft)
{
pSoftware = soft;
}
void Run()
{
cout << "在电脑上";
pSoftware->Run();
}
};
class Phone : public PlatForm
{
private:
Software *pSoftware;
public:
void SetSoftware(Software *soft)
{
pSoftware = soft;
}
void Run()
{
cout << "在手机上";
pSoftware->Run();
}
};
int main()
{
Software *pSoftwareA = new SoftwareA;
Software *pSoftwareB = new SoftwareB;
Phone *pPhone = new Phone;
PlatForm *pComputer = new Computer;
pComputer->SetSoftware(pSoftwareA);
pComputer->Run();
pComputer->SetSoftware(pSoftwareB);
pComputer->Run();
pPhone->SetSoftware(pSoftwareA);
pPhone->Run();
pPhone->SetSoftware(pSoftwareB);
pPhone->Run();
free(pComputer);
free(pPhone);
free(pSoftwareB);
free(pSoftwareA);
return 0;
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。