桥接模式与原理:将抽象部分与实现部分分离,使它们都可以独立的变化。最终的结果表现在实现类中。两者之间属于等价关系,即实现部分和抽象部分可以相互交换。
代码如下
#include <iostream>
using namespace std;
class OS
{
public:
virtual void install()
{ }
}; class WindowsOS:public OS
{
public:
virtual void install()
{
cout << "install windows OS" <<endl;
}
}; class LinuxOS:public OS
{
public:
virtual void install()
{
cout << "install linux OS" <<endl;
}
}; class Computer
{
public:
virtual void impInstall(OS* os)
{ }
}; class ComputerHP:public Computer
{
public:
virtual void impInstall(OS* os)
{
cout<<"I am ComputerHP" <<endl;
os->install();
}
private: }; class ComputerApple:public Computer
{
public:
virtual void impInstall(OS* os)
{
cout<<"I am ComputerApple" <<endl;
os->install();
}
}; int main()
{
OS* os = new LinuxOS();
Computer *pcomp = new ComputerApple();
pcomp->impInstall(os);
return 0;
};