2.当Adaptee中添加新的抽象方法时,Adapter类不需要做任何调整,也能正确的进行动作;
3.可以使用多肽的方式在Adapter类中调用Adaptee类子类的方法。
由于对象适配器的耦合度比较低,所以在很多的书中都建议使用对象适配器。在我们实际项目中,也是如此,能使用对象组合的方式,就不使用多继承的方式。
代码实现
类适配器的实现代码
复制代码
/*
** FileName : AdapterPatternDemo
** Author : Jelly Young
** Date : 2013/11/27
** Description : More information, please go to http://www.easck.com/> */
#include <iostream>
using namespace std;
// Targets
class Target
{
public:
virtual void Request()
{
cout<<"Target::Request"<<endl;
}
};
// Adaptee
class Adaptee
{
public:
void SpecificRequest()
{
cout<<"Adaptee::SpecificRequest"<<endl;
}
};
// Adapter
class Adapter : public Target, Adaptee
{
public:
void Request()
{
Adaptee::SpecificRequest();
}
};
// Client
int main(int argc, char *argv[])
{
Target *targetObj = new Adapter();
targetObj->Request();
delete targetObj;
targetObj = NULL;
return 0;
}
对象适配器的代码实现
复制代码
/*
** FileName : AdapterPatternDemo
** Author : Jelly Young
** Date : 2013/11/27
** Description : More information, please go to http://www.easck.com/> */
#include <iostream>
using namespace std;










