Adapter (C++实现 )
// Adapter.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
class Target
{
public :
Target() {}
virtual ~Target(){}
virtual void Request()=0;
};
class Adaptee
{
public:
Adaptee(){}
virtual ~Adaptee(){}
void SpecialRequset()
{
cout<<"SpecialRequset of Adaptee"<<endl;
}
};
//////////////////////////////////////////////////////////////
class Adapter_class:private Adaptee,public Target
{
public:
Adapter_class()
{
cout<<"Construction of Adapter_class"<<endl;
}
virtual ~Adapter_class()
{
cout<<"Destruction of Adapter_class"<<endl;
}
void Request()
{
SpecialRequset();
}
};
//////////////////////////////////////////////////////////////
class Adpater_object:public Target
{
public:
Adpater_object(Adaptee* m_p):m_pAdaptee(m_p)
{
cout<<"Construction of Adapter_objetc"<<endl;
}
virtual ~Adpater_object()
{
delete m_pAdaptee;
m_pAdaptee=NULL;
cout<<"Destruction of Adapter_class"<<endl;
}
void Request()
{
m_pAdaptee->SpecialRequset();
}
private:
Adaptee* m_pAdaptee;
};
int _tmain(int argc, _TCHAR* argv[])
{ //Client1
Target *t=new Adapter_class();
t->Request();
delete t;
//Client2
Adaptee *p=new Adaptee();
Target *t1=new Adpater_object(p);
t1->Request();
return 0;
}