Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of Proxy design pattern

I tried to understand proxy design pattern. But i could not understand the usage of the proxy design pattern. i got this code example 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 loading is unnecessary for second time of dispalyImage. Even it is possible in directly accessing the RealImage object too.

            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 usage of the ProxyImage class in this pattern.

like image 972
Buru Avatar asked Jan 20 '15 19:01

Buru


2 Answers

Proxy means ‘in place of’, representing’ or the authority to represent someone else, or a figure that can be used to represent the value of something. Proxy design pattern is also called surrogate, handle, and wrapper.

It is used when we want to create a wrapper to cover the main object's complexity from the client.

Some real world examples of Proxy Design Pattern:

  1. A bank's cheque or credit card is a proxy for what is in our bank account. It can be used in place of cash, and provides a means of accessing that cash when required. And that’s exactly what the Proxy pattern does – “Controls and manage access to the object they are protecting“.

  2. A company or corporate used to have a proxy which restricts few site access. The proxy first checks the host you are connecting to, if it is not a part of restricted site list, then it connects to the real internet.

like image 179
Gul Ershad Avatar answered Nov 09 '22 20:11

Gul Ershad


You know, I agree with you. I feel like there's a much better example they could have used for the proxy pattern. This seems to use the same example but it's explained much better. You should look at that instead.

Basically, it all comes down to this comment:

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

That is the benefit the proxy gives you in this example. If you don't display the image, you don't pay the penalty of loading it:

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

    }

}
like image 26
Daniel Kaplan Avatar answered Nov 09 '22 20:11

Daniel Kaplan