标签:c++ 设计模式
#include "stdafx.h" #include <iostream> #include <memory> using namespace std; class Subject { public : virtual void Request() = 0; }; class RealSubject :public Subject { public : virtual void Request() { cout << "真实的操作:RealSubject" << endl; } }; class Proxy :public Subject { private : shared_ptr <RealSubject > _pRealSubject; public : virtual void Request() { if (_pRealSubject == NULL) _pRealSubject = shared_ptr <RealSubject >(new RealSubject ); cout << "可以做一些内务处理!" << endl; _pRealSubject->Request(); } };
// ProxyMethod.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "Proxy.h" int _tmain (int argc , _TCHAR * argv []) { shared_ptr <Proxy > pProxy( new Proxy()); pProxy->Request(); getchar(); return 0; }运行结果:
标签:c++ 设计模式
原文地址:http://blog.csdn.net/wwwdongzi/article/details/26349749