代理模式 - 加载到内存中

时间:2013-04-24 19:41:34

标签: java proxy-classes

我正在查看解释代理模式的代码示例。这是代码:

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

}

}

假设代理模式正确实现,这是该程序的主要方法。这就是我想知道的:代码中的注释说当我们使用代理图像对象时,如果我们将图片加载到内存中,那么只加载该图像。但是如果我们不使用代理并直接创建真实图像,当我们加载这个类的实例时,我们将该类的所有实例加载到内存中。我不明白为什么会这样。是的,代理模式的全部要点就是这样做,但是当我们调用highResolutionImageNoProxy2.showImage()时,我不明白为什么所有3个highResolutionImageNoProxy对象都被加载到内存中。 。任何人都可以解释一下吗?

由于

编辑:我想我弄明白了为什么。因为ImageProxy类只在尝试对对象执行操作时才调用HighResolutionImage类的构造函数,但是如果我们直接创建HighResolutionImage,那么因为它的构造函数创建了对象,所以它们都被加载到内存中。

1 个答案:

答案 0 :(得分:2)

代码假定在创建HighResolutionImage的实例时,即使showImage()未被调用,图像也会加载到内存中。

代理将确保仅在调用showImage()时才将图像加载到内存中。

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