#include #include using namespace std; class Subject{ public: virtual string Request()=0; }; class RealSubject:public Subject{ public: string Request(){ return "Answer from Real Subject"; } }; class CacheProxy: public Subject{ RealSubject realSubject; string answer; public: string Request(){ if (answer.empty()){ answer = realSubject.Request(); cout<<"CacheProxy::Request from Real Subject"<Request(); } ~VirtualProxy(){ if(subject) delete subject; } }; class ProtectionProxy: public Subject{ //An Authentication Proxy first asks for a password RealSubject *subject; string password="abc"; public: string Authenticate(string supplied){ if (subject) return "Protection Proxy: You already allowed to access"; if (supplied!=password) return "Protection Proxy: No access"; subject = new RealSubject(); return "Protection Proxy: Authenticated"; } string Request(){ if (!subject) return "Protection Proxy: Authenticate first"; return "Protection Proxy: Call to " + subject->Request(); } ~ProtectionProxy(){ if(subject) delete subject; } }; int main(){ Subject *subject = new VirtualProxy(); cout<Request()<Request()<Request()<Authenticate("WrongPassword")<Authenticate("abc")<Authenticate("abc")<Request()<Request()<Request()<Request()<