适配器模式
将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。适配器模式讲了两种类型,类适配器模式和对象适配器模式。
本文使用书中篮球翻译的例子,代码使用C++语言描述,代码存在的不足或问题有望各位指出。
(1)对象适配器模式模版
//对象适配器模式
#include <iostream>
using namespace std;
class Target
{
public:
Target(){}
virtual ~Target(){}
virtual void request()
{
cout <<"普通请求!"<<endl;
}
};
class Adaptee
{
public:
void specificRequest()
{
cout <<"特殊的请求!"<<endl;
}
};
class Adapter :public Target
{
public:
Adapter(Adaptee *adaptee)
{
this->adaptee = adaptee;
}
~Adapter()
{
}
void request() override
{
adaptee->specificRequest();
}
private:
Adaptee *adaptee;
};
int main()
{
Adaptee* adaptee = new Adaptee();
Target *target=new Adapter(adaptee);
target->request();
delete adaptee;
delete target;
return 0;
}
(2)类适配器模式模版
//类适配器模式
#include <iostream>
using namespace std;
class Target
{
public:
Target(){}
virtual ~Target(){}
virtual void request()
{
cout <<"普通请求!"<<endl;
}
};
class Adaptee
{
public:
void specificRequest()
{
cout <<"特殊的请求!"<<endl;
}
};
class Adapter :public Target,private Adaptee
{
public:
Adapter()
{
}
~Adapter()
{
}
void request() override
{
this->specificRequest();
}
private:
};
int main()
{
Target *target=new Adapter();
target->request();
delete target;
return 0;
}
(3)篮球翻译事例使用对象适配器模式实习
#include <iostream>
#include <string>
#include <string>
using namespace std;
class Player
{
public:
Player(){}
virtual ~Player(){}
void setName(string)
{
this->name = name;
}
virtual void Attack()=0;
virtual void Defense()=0;
protected:
string name;
};
class Forwards:public Player
{
public:
Forwards(){}
~Forwards(){}
Forwards(string name)
{
this->name = name;
}
void Attack() override
{
cout << "前锋 "<< name << " 进攻" << endl;
}
void Defense() override
{
cout << "前锋 " << name <<" 防守" << endl;
}
};
class Center: public Player
{
public:
Center(){}
~Center(){}
Center(string name)
{
this->name = name;
}
void Attack() override
{
cout << "中峰 "<< name << " 进攻" << endl;
}
void Defense() override
{
cout << "中锋 " << name <<" 防守" << endl;
}
};
class Guards: public Player
{
public:
Guards(){}
~Guards(){}
Guards(string name)
{
this->name = name;
}
void Attack() override
{
cout << "后卫 "<< name << " 进攻" << endl;
}
void Defense() override
{
cout << "后卫 " << name <<" 防守" << endl;
}
};
class ForeignCenter
{
public:
ForeignCenter(){}
~ForeignCenter(){}
void setName(string name)
{
this->name = name;
}
void attack()
{
cout <<"外籍中锋 " << name << " 进攻" << endl;
}
void defense()
{
cout <<"外籍中锋 " << name << " 防守" << endl;
}
private:
string name;
};
class Translator: public Player
{
public:
Translator(){}
~Translator()
{
delete foreignCenter;
}
Translator(string name)
{
this->name = name;
foreignCenter->setName(name);
}
void Attack() override
{
foreignCenter->attack();
}
void Defense() override
{
foreignCenter->defense();
}
private:
ForeignCenter *foreignCenter = new ForeignCenter();
};
int main()
{
Player *b = new Forwards("巴蒂尔");
b->Attack();
Player *m = new Guards("麦克格雷迪");
m->Attack();
Player *ym = new Translator("姚明");
ym->Attack();
ym->Defense();
return 0;
}
使用适配器模式
系统的数据和行为都正确,但接口不符时,我们应该考虑用适配器,目的是使控制范围之外的一个原有对象与某个接口匹配。适配器模式主要应用于希望复用一些现存的类,但是接口又与复用环境要求不一致的情况。