/* lazyproxy.cpp * * Example of Proxy Pattern. */ #include class Subject { public: virtual void operation() = 0; }; class RealSubject: public Subject { public: virtual void operation() { std::cout << "RealSubject operation\n"; } }; class Proxy: public Subject { public: Proxy() : p_real_subject(0) {} virtual void operation() { std::cout << "Proxy operation\n"; if (!p_real_subject) p_real_subject = new RealSubject; p_real_subject->operation(); } ~Proxy() { if (p_real_subject) delete p_real_subject; } private: RealSubject* p_real_subject; }; class Client { public: Client(Subject& subject) : _subject(subject) {} virtual void do_something() { _subject.operation(); } private: Subject& _subject; }; int main() { Proxy proxy; Client client(proxy); client.do_something(); }