I tried to understand the proxy pattern. But I could not understand the use of the proxy template. I got this sample code from wikipedia
interface Image {
public void displayImage();
}
class RealImage implements Image {
private String filename = null;
public RealImage(final String filename) {
this.filename = filename;
loadImageFromDisk();
}
private void loadImageFromDisk() {
System.out.println("Loading " + filename);
}
public void displayImage() {
System.out.println("Displaying " + filename);
}
}
class ProxyImage implements Image {
private RealImage image = null;
private String filename = null;
public ProxyImage(final String filename) {
this.filename = filename;
}
public void displayImage() {
if (image == null) {
image = new RealImage(filename);
}
image.displayImage();
}
}
class ProxyExample {
public static void main(String[] args) {
final Image IMAGE1 = new ProxyImage("HiRes_10MB_Photo1");
final Image IMAGE2 = new ProxyImage("HiRes_10MB_Photo2");
IMAGE1.displayImage();
IMAGE1.displayImage();
IMAGE2.displayImage();
IMAGE2.displayImage();
IMAGE1.displayImage();
}
}
In this example, they said that loading is not required a second time dispalyImage. Even this is possible with direct access to the RealImage object.
final Image IMAGE1 = new RealImage("HiRes_10MB_Photo1");
final Image IMAGE2 = new RealImage("HiRes_10MB_Photo2");
IMAGE1.displayImage();
IMAGE1.displayImage();
IMAGE2.displayImage();
IMAGE2.displayImage();
IMAGE1.displayImage();
I need to understand the use of the ProxyImage class in this template.
source
share