代理模式,对象适配器模式的区别:
适配器模式:适配者与被适配者接口不同,为了是用户使用同一的接口。
代理模式:代理者与被代理者接口相同,只是客户不能访问到被代理者。
#include <iostream>
using namespace std;
//
struct IImage
{
public:
IImage(string name) : m_name(name) { }
virtual ~IImage() { }
virtual void Show() = 0;
protected:
string m_name;
};
class CBigImage : public IImage
{
public:
CBigImage(string name) : IImage(name) { }
virtual ~CBigImage() { }
virtual void Show() { cout<<m_name.c_str()<<endl; }
};
class CBigImageProxy : public IImage
{
public:
CBigImageProxy(string name) : IImage(name)
{
m_pBigImage = new CBigImage(m_name);
}
virtual ~CBigImageProxy()
{
if(m_pBigImage)
{
delete m_pBigImage;
m_pBigImage = NULL;
}
}
virtual void Show()
{
m_pBigImage->Show();
}
private:
CBigImage *m_pBigImage;
};
//
void main()
{
CBigImageProxy bigImageProxy("Big Image.");
bigImageProxy.Show();
}