代理设计模式的使用

Bur*_*man 5 java design-patterns

我试图理解代理设计模式.但我无法理解代理设计模式的用法.我从维基百科得到了这个代码示例

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
    }

}
Run Code Online (Sandbox Code Playgroud)

在这个例子中,他们说第二次dispalyImage不需要加载.甚至可以直接访问RealImage对象.

            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
Run Code Online (Sandbox Code Playgroud)

我需要了解此模式中ProxyImage类的用法.

Dan*_*lan 10

你知道,我同意你的看法.我觉得有一个很多他们可以使用代理模式更好的例子. 这似乎使用相同的例子,但它解释得更好. 你应该看看它.

基本上,这一切都归结为这个评论:

// create the Image Object only when the image is required to be shown
Run Code Online (Sandbox Code Playgroud)

这是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

    }

}
Run Code Online (Sandbox Code Playgroud)

  • 已经有一段时间了,但我建议将其标记为答案 (2认同)

Gul*_*had 5

代理的意思是\xe2\x80\x98代替\xe2\x80\x99,代表\xe2\x80\x99或者代表别人的权限,或者可以用来代表某物的价值的数字。\n代理设计模式也称为代理、句柄和包装器。

\n

当我们想要创建一个包装器来覆盖客户端的主要对象的复杂性时,可以使用它。

\n

代理设计模式的一些现实世界示例:

\n
    \n
  1. 银行支票或信用卡是我们银行账户中资金的代理。它可以代替现金使用,并提供在需要时获取现金的方式。\xe2\x80\x99 正是代理模式所做的 \xe2\x80\x93 \xe2\x80\x9c 控制和管理对其保护的对象的访问\xe2\x80\x9c。

    \n
  2. \n
  3. 公司或企业曾经拥有限制少数网站访问的代理。代理首先检查您要连接的主机,如果它不属于受限站点列表,则它会连接到真实的互联网。

    \n
  4. \n
\n