【设计模式】在NBA需要翻译 --- 适配器模式
来源:互联网 发布:win7 网络连接 感叹号 编辑:程序博客网 时间:2024/06/05 16:15
一,概述
适配器模式(有时候也称包装样式或者包装)将一个类的接口适配成用户所期待的。一个适配允许通常因为接口不兼容而不能在一起工作的类工作在一起,做法是将类自己的接口包裹在一个已存在的类中。
二,设配器基本构成
Target:客户期待的接口(可以是具体的或抽象的类)
Adaptee:需要适配的类(用户要调用,但是不能直接调用)
Adapter:设配器(继承用户所期待的Target,然后根据用户期待的接口适配调用真正需要调用的类Adaptee)
#include <iostream>using namespace std;class Target{public:virtual void Request(){ cout<<"普通请求"<<endl; }};class Adaptee{public:void SpecificRequest() { cout<<"特殊请求"<<endl; }};class Adapter : public Target//记住这里是共有继承 { private: Adaptee *adaptee;// = new Adaptee(); public: void Request() { adaptee->SpecificRequest(); }}; int main(){Target *target = new Adapter();target->Request();}
三,适配器示例
在NBA,教练用英语布置战术,姚明听不懂,需要翻译给解释听。就是把教练接口用设配器转化成姚明可以听懂的汉语。
#include <iostream>using namespace std; //篮球运动员 class Player { protected: string name; public: Player(string name) { this->name = name; } virtual void Attack()=0; virtual void Defense()=0; }; //前锋 class Forwards :public Player { public: Forwards(string name): Player(name) { } void Attack() { cout<<"前锋"<<name<<"进攻"<<endl; } void Defense() { cout<<"前锋"<<name<<"防守"<<endl; } }; //中锋 class Center :public Player { public: Center(string name):Player(name) { } void Attack() { cout<<"中锋"<<name<<"进攻"<<endl; } void Defense() { cout<<"中锋"<<name<<"防守"<<endl; } }; //后卫 class Guards :public Player { public: Guards(string name):Player(name) { } void Attack() { cout<<"后卫"<<name<<"进攻"<<endl; } void Defense() { cout<<"后卫"<<name<<"防守"<<endl; } }; //外籍中锋 class ForeignCenter { //private: public: string name; string getName() { return name; } void setName(string value) { this->name = value; } void China_attack() { cout<<"外籍中锋"<<name<<"进攻"<<endl; } void Chian_defence() { cout<<"外籍中锋"<<name<<"防守"<<endl; } }; //翻译者 class Translator : public Player { private: ForeignCenter *wjzf;// = new ForeignCenter(); public: Translator(string name): Player(name) { wjzf = new ForeignCenter();//必须申请对象 wjzf->name = name; } void Attack() { wjzf->China_attack(); } void Defense() { wjzf->Chian_defence(); } }; int main() { Player *b = new Forwards("巴蒂尔"); b->Attack(); Player *m = new Guards("麦克格雷迪"); m->Attack(); //Player ym = new Center("姚明"); Player *ym = new Translator("姚明"); ym->Attack(); ym->Defense(); return 0; }
- 【设计模式】在NBA需要翻译 --- 适配器模式
- 《大话设计模式》之--第17章 在NBA我需要翻译----适配器模式
- 《大话设计模式》之--第17章 在NBA我需要翻译----适配器模式
- 《大话设计模式》--在NBA我需要翻译--适配器模式<Adapter>(17)
- 大话设计模式-第17章 在NBA我需要翻译----适配器模式
- 第17章 在NBA我需要翻译--适配器模式
- 第十七章 在看NBA需要翻译--适配器模式(读书笔记)
- 【结构型模式】《大话设计模式》——读后感 (12)在NBA我需要翻译?——适配器模式
- 《大话设计模式》—— 读后感(17)在NBA我需要翻译——适配器模式
- 设计模式——适配器模式(NBA里有翻译)
- 适配器模式 X NBA翻译 X 接口不一致
- 适配器模式---出国需要一个翻译
- 设计模式:适配器模式
- 设计模式------适配器模式
- 设计模式:适配器模式
- 设计模式---适配器模式
- 设计模式 适配器模式
- 设计模式 - 适配器模式
- otsu二值化
- 手机分辨率分类
- linux下自动修复mysql数据库和表
- InvokeRequired 属性 与Invoke方法
- Linux Epoll介绍和程序实例
- 【设计模式】在NBA需要翻译 --- 适配器模式
- 【开源】完美破解九宫格(数独)游戏
- kthread_run 学习
- 利用jdk1.6 获取服务器ip和mac
- bin目录下的Release和Debug与obj目录下的Release和Debug的区别
- C# 中文数据导出CSV出现乱码解决方法
- 黑马程序员--Java学习日记10_交通灯系统
- 软件-eclipse
- BUGS