为其他对象提供一种代理以控制(隔离,使用接口)对这个对象的访问。
有四种常用的情况:
简单的虚拟代理实现:
class Image { public: Image(string name): m_imageName(name) {} virtual ~Image() {} virtual void Show() {} protected: string m_imageName; }; class BigImage: public Image { public: BigImage(string name):Image(name) {} ~BigImage() {} void Show() { cout<<"Show big image : "<<m_imageName<<endl; } }; class BigImageProxy: public Image { private: BigImage *m_bigImage; public: BigImageProxy(string name):Image(name),m_bigImage(0) {} ~BigImageProxy() { delete m_bigImage; } void Show() { if(m_bigImage == NULL) m_bigImage = new BigImage(m_imageName); m_bigImage->Show(); } };
客户调用:
int main() { Image *image = new BigImageProxy("proxy.jpg"); //代理 image->Show(); //需要时由代理负责打开 delete image; return 0; }