gpt4 book ai didi

java - 代理模式 - 加载到内存中

转载 作者:行者123 更新时间:2023-11-30 04:24:52 25 4
gpt4 key购买 nike

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

/**
* 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,那么由于它的构造函数创建了该对象,所以它们都会被加载到内存中。

最佳答案

代码假设当您创建 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();

关于java - 代理模式 - 加载到内存中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16200787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com