The Virtual Proxy pattern is a memory saving technique that recommends postponing an object creation until it is needed:
The object is created the first time it is referenced in the application and the same instance is reused from that point onwards. This approach has advantages and disadvantages.
Advantage
The advantage of this approach is a faster application start-up time, as it is not required to create and load all of the application objects.
Disadvantage
Because there is no guarantee that a given application object is created, everywhere the application object is accessed it needs to be checked to make sure that it is not null, i.e., the object is created. The time penalty associated with this check is the main disadvantage.
Applying the Virtual Proxy pattern, a separate object referred to as a virtual proxy can be designed with its interface the same as that of the actual object. Different client objects can create an instance of the corresponding virtual proxy and use it in place of the actual object. The Virtual Proxy object maintains a reference to the actual object as one of its instance variables.
The proxy does not automatically create the actual object. When a client invokes a method on the Virtual Proxy object that requires the services of the actual object, it checks to see if the actual object is created.
- If the actual object is already created, the proxy forwards the method call to the actual object.
- If the actual object is not already created:
– It creates the actual object.
– It assigns the object to its object reference variable.
– It forwards the call to the actual object.
(virtual_proxy)
source for the RealProcessor
public class RealProcessor extends IDEOperation {JavaDoc jdoc;public RealProcessor() {super();jdoc = new JavaDoc();}public void generateDocs(String javaFile) {jdoc.generateDocs(javaFile);}}
public class ProxyProcessor extends IDEOperation {
private RealProcessor realProcessor;
public void generateDocs(String javaFile) {
/*
In order to generate javadocs
the proxy loads the actual object and
invokes its methods.
*/
if (realProcessor == null) {
realProcessor = new RealProcessor();
}
realProcessor.generateDocs(javaFile);
}
}
NOTE : ADD THE ADDITIONAL NOTES
No comments:
Post a Comment