Welcome to tiger's blog!

What lead to success, what we are seeking...
posts - 47, comments - 23, trackbacks - 0, articles - 8
   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

设计模式(Adapter)

Posted on 2007-04-13 09:47 tiger 阅读(322) 评论(0)  编辑 收藏 引用

//类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;
}


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理