首页 > 代码库 > 适配器模式之C++实现
适配器模式之C++实现
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
//Target
class ForeignMovie
{
public:
virtual void ShowSubtitle() = 0;
};
//中文字幕
class ChineseSubtitle
{
public:
void ShowChineseSubtitle()
{
cout << "显示中文字幕" << endl;
}
};
//外文电影默认显示外文字幕,如果要显示中文字幕,需要翻译适配
class Translate : public ForeignMovie
{
private:
ChineseSubtitle *pChineseSubtitle;
public:
Translate()
{
pChineseSubtitle = new ChineseSubtitle;
}
void ShowSubtitle()
{
pChineseSubtitle->ShowChineseSubtitle();
}
};
int main()
{
Translate *pTranslate = new Translate;
pTranslate->ShowSubtitle();
return 0;
}
#include <string>
#include <iostream>
using namespace std;
//Target
class ForeignMovie
{
public:
virtual void ShowSubtitle() = 0;
};
//中文字幕
class ChineseSubtitle
{
public:
void ShowChineseSubtitle()
{
cout << "显示中文字幕" << endl;
}
};
//外文电影默认显示外文字幕,如果要显示中文字幕,需要翻译适配
class Translate : public ForeignMovie
{
private:
ChineseSubtitle *pChineseSubtitle;
public:
Translate()
{
pChineseSubtitle = new ChineseSubtitle;
}
void ShowSubtitle()
{
pChineseSubtitle->ShowChineseSubtitle();
}
};
int main()
{
Translate *pTranslate = new Translate;
pTranslate->ShowSubtitle();
return 0;
}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。