Complete the code to declare the Proxy class implementing the Subject interface.
class Proxy implements [1] { private RealSubject realSubject; public void request() { if (realSubject == null) { realSubject = new RealSubject(); } realSubject.request(); } }
The Proxy class must implement the Subject interface to act as a substitute for RealSubject.
Complete the code to forward the request from Proxy to RealSubject.
public void request() {
if (realSubject == null) {
realSubject = new RealSubject();
}
realSubject.[1]();
}The Proxy forwards the call to the RealSubject's request() method.
Fix the error in the Proxy constructor to initialize the RealSubject lazily.
public Proxy() {
[1] = null;
}The instance variable realSubject should be set to null to delay creation.
Fill both blanks to add access control in Proxy before forwarding the request.
public void request() {
if ([1]()) {
realSubject.[2]();
} else {
System.out.println("Access denied.");
}
}The Proxy checks access with checkAccess() before calling realSubject.request().
Fill all three blanks to implement caching in Proxy for the result of RealSubject's request.
private String cache;
public String request() {
if (cache == null) {
cache = realSubject.[1]();
}
return [2];
}
public void clearCache() {
[3] = null;
}The Proxy caches the result of realSubject.request() in cache and returns it. clearCache() resets cache to null.