设计模式(Adapter)

//类Adapter
#include <iostream>
using namespace std;

class CAdaptee
{
public:
 CAdaptee(){}
 virtual ~CAdaptee(){}

public:
 void Func1(){cout<<"CAdaptee"<<endl;}

};

class CTarget
{
public:
 CTarget(){}
 virtual ~CTarget(){}

public:
 virtual void Func() = 0;

};

class CAdapter : public CTarget, private CAdaptee
{
public:
 CAdapter(){}
 virtual ~CAdapter(){}

public:
 void Func(){Func1();}

};

void main()
{
 CTarget *pTarget = new CAdapter();
 pTarget->Func();
 delete pTarget;
}


//对象Adapter(组合)
#include <iostream>
using namespace std;

class CAdaptee
{
public:
 CAdaptee(){}
 virtual ~CAdaptee(){}

public:
 void Func1(){cout<<"CAdaptee"<<endl;}

};

class CTarget
{
public:
 CTarget(){}
 virtual ~CTarget(){}

public:
 virtual void Func() = 0;

};

class CAdapter : public CTarget
{
public:
 CAdapter(){}
 virtual ~CAdapter(){}

public:
 void Func(){m_Adaptee.Func1();}

private:
 CAdaptee m_Adaptee;

};

void main()
{
 CTarget *pTarget = new CAdapter();
 pTarget->Func();
 delete pTarget;
}

//对象Adapter(参数)
#include <iostream>
using namespace std;

class CAdaptee
{
public:
 CAdaptee(){}
 virtual ~CAdaptee(){}

public:
 void Func1(){cout<<"CAdaptee"<<endl;}

};

class CTarget
{
public:
 CTarget(){}
 virtual ~CTarget(){}

public:
 virtual void Func() = 0;

};

class CAdapter : public CTarget
{
public:
 CAdapter(CAdaptee *pAdaptee){m_pAdaptee = pAdaptee;}
 virtual ~CAdapter(){}

public:
 void Func(){m_pAdaptee->Func1();}

private:
 CAdaptee *m_pAdaptee;

};

void main()
{
 CAdaptee *pAdaptee = new CAdaptee();
 CTarget *pTarget = new CAdapter(pAdaptee);
 pTarget->Func();
 delete pTarget;
 delete pAdaptee;
}

你可能感兴趣的:(设计模式(Adapter))