首页 > 代码库 > 桥接模式之C++实现
桥接模式之C++实现
#include "stdafx.h"
#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;
}
#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;
}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。