Question

I am looking at a code sample that explains the proxy pattern. Here is the code:

/**
* Proxy
*/
public class ImageProxy implements Image {

/**
 * Private Proxy data 
 */
private String imageFilePath;

/**
 * Reference to RealSubject
 */
private Image proxifiedImage;


public ImageProxy(String imageFilePath) {
    this.imageFilePath= imageFilePath;  
}

@Override
public void showImage() {

    // create the Image Object only when the image is required to be shown

    proxifiedImage = new HighResolutionImage(imageFilePath);

    // now call showImage on realSubject
    proxifiedImage.showImage();

}

}

/**
 * RealSubject
 */
public class HighResolutionImage implements Image {

public HighResolutionImage(String imageFilePath) {

    loadImage(imageFilePath);
}

private void loadImage(String imageFilePath) {

    // load Image from disk into memory
    // this is heavy and costly operation
}

@Override
public void showImage() {

    // Actual Image rendering logic

}

}

/**
  * Image Viewer program
 */
public class ImageViewer {


public static void main(String[] args) {

// assuming that the user selects a folder that has 3 images    
//create the 3 images   
Image highResolutionImage1 = new ImageProxy("sample/veryHighResPhoto1.jpeg");
Image highResolutionImage2 = new ImageProxy("sample/veryHighResPhoto2.jpeg");
Image highResolutionImage3 = new ImageProxy("sample/veryHighResPhoto3.jpeg");

// assume that the user clicks on Image one item in a list
// this would cause the program to call showImage() for that image only
// note that in this case only image one was loaded into memory
highResolutionImage1.showImage();

// consider using the high resolution image object directly
Image highResolutionImageNoProxy1 = new HighResolutionImage("sample/veryHighResPhoto1.jpeg");
Image highResolutionImageNoProxy2 = new HighResolutionImage("sample/veryHighResPhoto2.jpeg");
Image highResolutionImageBoProxy3 = new HighResolutionImage("sample/veryHighResPhoto3.jpeg");


// assume that the user selects image two item from images list
highResolutionImageNoProxy2.showImage();

// note that in this case all images have been loaded into memory 
// and not all have been actually displayed
// this is a waste of memory resources

}

}

Assume proxy pattern is implemented correctly, and this is the main method of the program. Here is what i wonder: The comments in the code say that when we use proxy image objects, if we load a picture into memory, just that image is loaded. But if we do not use proxy and directly create real images, when we load an instance of this class, we load all instances of the class into memory. I do not understand why this is the case. Yes, the whole point of proxy pattern is to do this, but i do not understand why all 3 of the highResolutionImageNoProxy objects are loaded into memory when we call highResolutionImageNoProxy2.showImage(); . Can anyonbody explain it?

Thanks

Edit: I think i figured out why. Because the ImageProxy class calls the constructor of HighResolutionImage class only when it tries to do an operation on the object, but if we create a HighResolutionImage directly, then since its constructor creates the object, all of them are loaded into memory.

Was it helpful?

Solution

The code assumes that when you create an instance of HighResolutionImage, the image is loaded to the memory, even if showImage() isn't called.

The proxy will assure that the image is loaded to the memory only when showImage() is called.

//load veryHighResPhoto1 to memory
Image highResolutionImageNoProxy1 = new HighResolutionImage("sample/veryHighResPhoto1.jpeg");
//load veryHighResPhoto2 to memory
Image highResolutionImageNoProxy2 = new HighResolutionImage("sample/veryHighResPhoto2.jpeg");
//load veryHighResPhoto3 to memory
Image highResolutionImageBoProxy3 = new HighResolutionImage("sample/veryHighResPhoto3.jpeg");

//load just the proxys (image not loaded yet)
Image highResolutionImage1 = new ImageProxy("sample/veryHighResPhoto1.jpeg");
Image highResolutionImage2 = new ImageProxy("sample/veryHighResPhoto2.jpeg");
Image highResolutionImage3 = new ImageProxy("sample/veryHighResPhoto3.jpeg");
//trigger the load of the image into memory
highResolutionImage1.showImage();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top