Core Java Design Pattern : Structural Pattern : Proxy Design Pattern
Proxy Design Pattern |
Provide a surrogate or placeholder for another object to control access to it.
Also Known As :Surrogate (A person appointed to represent or act on behalf of others)
Use the Proxy pattern when
- there is a need for a more versatile or sophisticated reference to an object than a simple pointer.
One reason for controlling access to an object is to defer the full cost of its creation and initialization until we actually need to use it. Consider a document editor that can embed graphical objects in a document. Some graphical objects, like large raster images, can be expensive to create. But opening a document should be fast, so we should avoid creating all the expensive objects at once when the document is opened. This isn’t necessary anyway, because not all of these objects will be visible in the document at the same time.
The solution is to use another object, an image proxy, that acts as a stand-in for the real image. The proxy acts just like the image and takes care of instantiating it when it’s required.
The document editor accesses embedded images through the interface defined by the abstract Graphic class. ImageProxy is a class for images that are created on demand. ImageProxy maintains the file name as a reference to the image on disk. The file name is passed as an argument to the ImageProxy constructor.
ImageProxy also stores the bounding box of the image and a reference to the real Image instance. This reference won’t be valid until the proxy instantiates the real image. The Draw operation makes sure the image is instantiated before forwarding it the request. GetExtent forwards the request to the image only if it’s instantiated; otherwise ImageProxy returns the extent it stores.
Examples |
Message.java
package com.javaskool;
import java.util.Date;
public abstract class Message {
public void sayHello() {
System.out.println(this.getClass().getSimpleName() + " says Hello how r u at " + new Date());
}
}
Mother.java
package com.javaskool;
public class Mother extends Message {
public Mother() {
}
}
Child.java
package com.javaskool;
public class Child extends Message {
public Child() {
try {
System.out.println("Child is Sleeping");
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Proxy.java
package com.javaskool;
import java.util.Date;
public class Proxy {
Child sleepingChild;
public Proxy() {
System.out.println("Creating proxy at " + new Date());
}
public void sayHello() {
if (sleepingChild == null) {
sleepingChild = new Child();
}
System.out.println("From Proxy : ");
sleepingChild.sayHello();
}
}
TestDrive.java
package com.javaskool;
public class TestDrive {
public static void main(String[] args) {
Proxy childproxy = new Proxy();//for the child object
Mother mom = new Mother();
mom.sayHello();
childproxy.sayHello();
}
}
Output
c:\>javac -d . *.java
c:\>java com.javaskool.TestDrive
Creating proxy at Fri Feb 14 00:38:11 IST 2014
Mother says Hello how r u at Fri Feb 14 00:38:15 IST 2014
Child is Sleeping
From Proxy :
Child says Hello how r u at Fri Feb 14 00:38:20 IST 2014
Recent Comments