今天在看<<设计模式精解-GoF 23 种设计模式解析附 C++实现源码>>中的Adapter模式时,作者的理解没有错,但在代码上的实现有点异议
他用了类模式的Adapter与对象模式的Adapter的代码,以实现统一接口,但运行时,没有实现动态得到Request,并没有达到实现Adapter的目的,具体源码可参考:
http://www.cnblogs.com/k-eckel/articles/188896.html实现也可参考java的Adapter采用接口方式(有空再试验)。
下面是C++实现的一下样例,可供参考:
#include "stdafx.h"
#include
using namespace std;
class ExecuteInterface {
public:
// 1. Specify the new i/f
virtual ~ExecuteInterface(){}
virtual void execute() = 0;
};
template // 2. Design a "wrapper" or
class ExecuteAdapter: public ExecuteInterface {
// "adapter" class
public:
ExecuteAdapter(TYPE *o, void(TYPE:: *m)())
{
object = o;
method = m;
}
~ExecuteAdapter()
{
delete object;
}
// 4. The adapter/wrapper "maps" the new to the legacy implementation
void execute() /* the new */
{
(object->*method)();
}
private:
TYPE *object; // ptr-to-object attribute
void(TYPE:: *method)(); /* the old */ // ptr-to-member-function
};
// attribute
// The old: three totally incompatible classes
// no common base class,
class Fea {
public:
// no hope of polymorphism
~Fea()
{
cout << "Fea::dtor" << endl;
}
void doThis()
{
cout << "Fea::doThis()" << endl;
}
};
class Feye {
public:~Feye()
{
cout << "Feye::dtor" << endl;
}
void doThat()
{
cout << "Feye::doThat()" << endl;
}
};
class Pheau {
public:
~Pheau()
{
cout << "Pheau::dtor" << endl;
}
void doTheOther()
{
cout << "Pheau::doTheOther()" << endl;
}
};
/* the new is returned */
ExecuteInterface **initialize() {
ExecuteInterface **array = new ExecuteInterface *[3]; /* the old is below */
array[0] = new ExecuteAdapter < Fea > (new Fea(), &Fea::doThis);
array[1] = new ExecuteAdapter < Feye > (new Feye(), &Feye::doThat);
array[2] = new ExecuteAdapter < Pheau > (new Pheau(), &Pheau::doTheOther);
return array;
}
int main10()
{
ExecuteInterface **objects = initialize();
for (int i = 0; i < 3; i++)
objects[i]->execute();
// 3. Client uses the new
// (polymporphism)
for (i = 0; i < 3; i++)
delete objects[i];
delete objects;
return 0;
}
参考:
功能 将一个类的接口转换成客户希望的另外一个接口,解决两个已有接口之间不匹配的问题。Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
下载是一个运行的真实实例(非示例,本人运行示例时浪费了点时间,希望别人少浪费时间)
/*********************************************/
using namespace std;
class T1
{
public:
T1(int i) { m_i = i; }
void Proc() {printf("T1 Proc %d\n", m_i);}
int m_i;
} ;
class T2
{
public:
T2(char c) { m_c = c; }
void Proc() {printf("T2 Proc %c\n", m_c);}
char m_c;
} ;
// class IAdaptor,抽象基类
class IAdaptor
{
public:
virtual void Proc() = 0 ;
};
// class Adaptor
template
class Adaptor : public IAdaptor, private T //实现继承
{
public:
Adaptor(int n) : T(n) {}
Adaptor(char c) : T(c) {}
virtual void Proc() { T::Proc() ; }
} ;
// 以统一方式调用函数Proc,而不关心是T1、T2或其他什么类
class Test
{
public:
Test(int n) : sp(new Adaptor< T1 >(n)) {}
Test(char c) : sp(new Adaptor < T2 >(c)) {}
void Proc() { sp->Proc() ; }
private:
std::auto_ptr< IAdaptor > sp ;
} ;
int main() {
Test t1(10) ;
t1.Proc() ;
Test t2('c') ;
t2.Proc() ;
return 0;
}
关键在于:
void Test(const std::auto_ptr& sp)
{
sp->Proc();
}
Test(std::auto_ptr(new Adaptor));
阅读(1561) | 评论(1) | 转发(0) |