プロキシ パターンを説明するコード サンプルを見ています。コードは次のとおりです。
/**
* 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 を直接作成すると、そのコンストラクターがオブジェクトを作成するため、それらすべてがメモリに読み込まれます。