Adapter 模式也叫适配器模式,是构造型模式之一,通过 Adapter 模式可以改变已有类(或外部类)的接口形式。
适用范围:是将一个类的接口转换成客户希望的另外一个接口。使得原本由于接口不兼容而不能
一起工作的那些类可以一起工作。
class NormalKv {
public:
virtual void useTargetKv() = 0;
};
class SpecialKv {
public:
void useCurrentKv() {
cout << "SpecialKv useCurrentKv Method----" << endl;
}
};
class Adapter :public NormalKv {
public:
Adapter(SpecialKv* speckv) {
m_specialKv = speckv;
}
void useTargetKv() {
m_specialKv->useCurrentKv();
}
private:
SpecialKv* m_specialKv;
};
int main() {
SpecialKv* speKv = new SpecialKv;
Adapter* adp = new Adapter(speKv);
adp->useTargetKv();
delete adp;
delete speKv;
system("pause");
return 0;
}