1. 适配器模式
适配器模式 (Adapter)将一个类的接口转换成客户希望的另外一个接口。
2. 适用场景
Adapter 模式使得原本由于接口不兼容而不能起工作的那些类可以一起工作。
- 在想使用一个已经存在的类,但如果它的接口,和现在的要求不相同时,应该考虑用适配器模式。
- 两个类所做的事情相同或相似,但是具有不同的接口时要使用它。
3. 结构图示
4. example
篮球比赛沟通,非中国国籍的人需要翻译。
一个抽象球员类,含有攻击防御两个抽象方法,分别有中锋 前锋 守门三种具体球员继承。
由于前锋有一个外籍人员(单独一个类实现),听不懂中文,所以需要一个翻译(适配器),该翻译类继承抽象球员类,实现相应的抽象方法封装外籍人员类相应的方法。
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
// 抽象球员类
class Player
{
string name;
public:
Player(string s) : name(s) {}
string getName(){return name;}
virtual void attack() = 0;
virtual void defense() = 0;
};
// 前锋
class Forwards : public Player
{
public:
Forwards(string name) : Player(name) {}
virtual void attack(){
cout << this->getName() << " 前锋 攻击" << endl;
}
virtual void defense() {
cout << this->getName() << " 前锋 防守" << endl;
}
};
// 中锋
class Center : public Player
{
public:
Center(string name) : Player(name) {}
virtual void attack(){
cout << this->getName() << " 中锋 攻击" << endl;
}
virtual void defense() {
cout << this->getName() << " 中锋 防守" << endl;
}
};
class Guard : public Player
{
public:
Guard(string name) : Player(name) {}
virtual void attack(){
cout << this->getName() << " 守门 攻击" << endl;
}
virtual void defense() {
cout << this->getName() << " 守门 防守" << endl;
}
};
// 非中国籍前锋
class Forwardsen {
string name;
public:
Forwardsen(string name) : name(name) {}
string getName(){return name;}
string setName(string s){name = s;}
virtual void attacken(){ // 外籍球员的接口和中国球员的不一样 使用的语言不一样
cout << name << " forwarder attack" << endl;
}
virtual void defenseen() {
cout << name << " forwarder defense" << endl;
}
};
// 用一个翻译(适配器),使用统一的接口将外籍人士相应的接口封装起来
class Translator : public Player{
Forwardsen *p;
public:
Translator(string name) : Player(name){
p = new Forwardsen(name);
}
virtual void attack(){
cout << "翻译为英文:";
p->attacken();
}
virtual void defense() {
cout << "翻译为英文:";
p->defenseen();
}
};
int main()
{
Player *p1 = new Forwards("小明");
Player *p2 = new Translator("englisher");
p1->attack();
p1->defense();
p2->attack();
p2->defense();
return 0;
}