代理设计模式 - tutorialspoint.com示例 [英] Proxy Design Pattern- tutorialspoint.com example
问题描述
http://www.tutorialspoint.com/design_pattern/proxy_pattern.htm
我正在寻找在java中使用
中的示例来了解代理设计模式链接上面。在主要的方法我不明白之间的区别:
I am looking to understand proxy design pattern in java using the example in the link above. In main method I don't understand the difference between:
//image will be loaded from disk
image.display();
System.out.println("");
//image will not be loaded from disk
image.display();
这是打字错误吗?如何相同的2 image.display()方法给出不同的输出?
Is this a typo? How can the same 2 image.display() methods give different outputs?
非常感谢!
推荐答案
这不是打字错误。如果您在本教程中查看 ProxyImage
的定义,则显示状态为:
This is not a typo. If you look in the definition of ProxyImage
in the tutorial, it clearly has state:
public class ProxyImage implements Image{
private RealImage realImage;
private String fileName;
public ProxyImage(String fileName){
this.fileName = fileName;
}
@Override
public void display() {
if(realImage == null){
realImage = new RealImage(fileName);
}
realImage.display();
}
}
当第一个致电, realImage
为null,图像将从磁盘加载。之后,将加载的图像存储到 image.realImage
并显示。在第二次调用时,该图像已被缓存,因此无需从磁盘载入。
When the first call is made, realImage
is null, and the image will be loaded from disk. After that, the loaded image is stored to image.realImage
and is displayed. At the second call that image is already cached, and hence no load from disk is necessary.
这篇关于代理设计模式 - tutorialspoint.com示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!