设计模式---适配器模式
来源:互联网 发布:算法的概念 上海教材 编辑:程序博客网 时间:2024/06/14 02:38
适配器模式
将一个类的接口转换成客户希望的另外一个接口。使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
#include<iostream>
using namespace std;
class Player
{
protected:
string m_name;
public:
Player(string name)
{
m_name = name;
}
virtual void Attack(){}
virtual void Defense(){}
};
class Forwards : public Player
{
public:
Forwards(string name):Player(name){}
void Attack()
{
cout<<"forwards attack..."<<m_name<<endl;
}
void Defense()
{
cout<<" forwars defense..."<<m_name<<endl;
}
};
class Center : public Player
{
public:
Center(string name):Player(name){}
void Attack()
{
cout<<"Center attack..."<<m_name<<endl;
}
void Defense()
{
cout<<" Center defense..."<<m_name<<endl;
}
};
class Guards : public Player
{
public:
Guards (string name):Player(name){}
void Attack()
{
cout<<"Guards attack..."<<m_name<<endl;
}
void Defense()
{
cout<<"Guards defense..."<<m_name<<endl;
}
};
class ForeignerCenter
{
private:
string m_name;
public:
void SetName(string name)
{
m_name = name;
}
string GetName()
{
return m_name;
}
void Attack()
{
cout<<"ForeignerCenter attack..."<<m_name<<endl;
}
void Defense()
{
cout<<"ForeignerCenter defense..."<<m_name<<endl;
}
};
class Translator:public Player
{
private:
ForeignerCenter *wf;
public:
Translator(string name):Player(name)
{
wf = new ForeignerCenter();
wf->SetName(m_name);
}
void Attack()
{
wf->Attack();
}
void Defense()
{
wf->Defense();
}
};
int main()
{
Player *b = new Forwards("bai");
b->Attack();
Player *m = new Guards("mai");
m->Attack();
Player *ym = new Translator("yao");
ym->Attack();
ym->Defense();
return 0;
}
阅读全文
0 0
- 设计模式:适配器模式
- 设计模式------适配器模式
- 设计模式:适配器模式
- 设计模式---适配器模式
- 设计模式 适配器模式
- 设计模式 - 适配器模式
- 设计模式--适配器模式
- 设计模式-适配器模式
- 设计模式------适配器模式
- 设计模式---适配器模式
- 设计模式- 适配器模式
- 设计模式 - 适配器模式
- 设计模式:适配器模式
- 适配器模式--设计模式
- 设计模式 - 适配器模式
- 设计模式-适配器模式
- 设计模式---适配器模式
- 设计模式-适配器模式
- 怎样根据某一列内容合并单元格——针对数据库的表头和表体
- mysql对比表结构对比同步,sqlyog架构同步工具
- 原码、反码、补码和移码其实很简单
- 区块链开发专题(比特币网络是如何构架起来的)
- MySQl-python模块,python连接数据库出现的问题
- 设计模式---适配器模式
- Excel表格的数据分拣
- python3中字符串与字节流的转换
- 手把手用Git将项目上传到GitHub上(一)
- POJ 3278 Catch That Cow【bfs】
- OpenSuSE services management
- OkHttp完全解析
- 好盈XERUN-V10有感无刷电机6根信号输出线
- 解决Android 中设置Clickable 有时无效的方法