设计模式-结构型-适配器
原创
©著作权归作者所有:来自51CTO博客作者shixin_0125的原创作品,请联系作者获取转载授权,否则将追究法律责任
#pragma once
#ifndef ADAPTER_H
#define ADAPTER_H
class Target
{
public:
Target(){}
virtual ~Target() {}
virtual void Request() = 0;
};
class Adaptee
{
public:
Adaptee(){}
~Adaptee(){}
void SpecialRequest();
};
class Adapter : public Target
{
public:
Adapter(Adaptee* pAdaptee);
virtual ~Adapter();
virtual void Request();
private:
Adaptee* m_pAdptee;
};
#endif
#include "StdAfx.h"
#include "adapter_impl.h"
#include <iostream>
void Adaptee::SpecialRequest()
{
std::cout << "SpecialRequest of Adaptee\n";
}
Adapter::Adapter(Adaptee* pAdaptee)
: m_pAdptee(pAdaptee)
{
}
Adapter::~Adapter()
{
delete m_pAdptee;
m_pAdptee = NULL;
}
void Adapter::Request()
{
std::cout << "Request of Adapter\n";
m_pAdptee->SpecialRequest();
}
#include "stdafx.h"
#include "adapter_impl.h"
#include <stdlib.h>
int _tmain(int argc, _TCHAR* argv[])
{
Adaptee *pAdaptee = new Adaptee;
Target *pTarget = new Adapter(pAdaptee);
pTarget->Request();
delete pTarget;
system("pause");
return 0;
}