Using the proxy design pattern

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();
}

//on System A 
class RealImage implements Image {

    private String filename = null;
    /**
     * Constructor
     * @param filename
     */
    public RealImage(final String filename) { 
        this.filename = filename;
        loadImageFromDisk();
    }

    /**
     * Loads the image from the disk
     */
    private void loadImageFromDisk() {
        System.out.println("Loading   " + filename);
    }

    /**
     * Displays the image
     */
    public void displayImage() { 
        System.out.println("Displaying " + filename); 
    }

}

//on System B 
class ProxyImage implements Image {

    private RealImage image = null;
    private String filename = null;
    /**
     * Constructor
     * @param filename 
     */
    public ProxyImage(final String filename) { 
        this.filename = filename; 
    }

    /**
     * Displays the image
     */
    public void displayImage() {
        if (image == null) {
           image = new RealImage(filename);
        } 
        image.displayImage();
    }

}

class ProxyExample {

   /**
    * Test method
    */
   public static void main(String[] args) {
        final Image IMAGE1 = new ProxyImage("HiRes_10MB_Photo1");
        final Image IMAGE2 = new ProxyImage("HiRes_10MB_Photo2");

        IMAGE1.displayImage(); // loading necessary
        IMAGE1.displayImage(); // loading unnecessary
        IMAGE2.displayImage(); // loading necessary
        IMAGE2.displayImage(); // loading unnecessary
        IMAGE1.displayImage(); // loading unnecessary
    }

}

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(); // loading necessary
            IMAGE1.displayImage(); // loading unnecessary
            IMAGE2.displayImage(); // loading necessary
            IMAGE2.displayImage(); // loading unnecessary
            IMAGE1.displayImage(); // loading unnecessary

I need to understand the use of the ProxyImage class in this template.

+4
source share
2 answers

You know, I agree with you. I feel that there is a much better example that they could use for a proxy template. This seems like the same example, but he explained much better. Instead, you should look at it.

, :

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

proxy. , :

package proxy;

/**
 * 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

    }

}
+9

"", - , , -. - , .

, , .

-:

1) - - , . , . , - " , ".

2) -, . , , , .

0

All Articles