从您发布的链接(强调我的):
代理类ProxyImage
在另一个系统上运行,而不是真实图像类本身,并且可以代表RealImage
那里的真实图像。从磁盘访问图像信息。使用代理模式,代码ProxyImage
避免了图像的多次加载,以节省内存的方式从其他系统访问它。
简而言之:它不会节省内存,它会加快应用程序的速度,因为您不需要每次都访问磁盘来读取真实图像。
这在这部分代码中得到了证明:
public void displayImage() {
//if image is not loaded into memory
if (image == null) {
//then load it, go to disk only once
image = new RealImage(filename);
}
//now it is in memory, display the real image
image.displayImage();
}
为了更好地理解这个问题,让我们更改类和接口的定义:
public interface Image {
String getName();
byte[] getData();
}
现在,RealImage
将始终在磁盘中寻找数据的类,以防文件不存在(它被删除或重命名):
public class RealImage implements Image {
//implements all the operations going to disk...
private String fileName;
public RealImage(String fileName) {
this.fileName = fileName;
}
@Override
public String getName() {
String name = "";
//fancy operations to seek for the file in disk (in case it has been deleted)
//read the data from file in disk
//get the name
name = ...;
return name;
}
@Override
public byte[] getData() {
byte[] data;
//again, fancy operations to seek for the file in disk (in case it has been deleted)
//read the data from file in disk
//get the image data for displaying purposes
data = ...;
return data;
}
}
现在,我们的商品ProxyImage
将充当代理,RealImage
通过将数据保存到内存中来节省每次访问磁盘的高成本任务:
public class ProxyImage implements Image {
private String fileName;
private RealImage realImage;
private byte[] data;
private String name;
//implements all the operations going to disk...
public RealImage(String fileName) {
this.fileName = fileName;
}
@Override
public String getName() {
//in case we don't have the name of the image
if (this.name == null) {
//use a RealImage to retrieve the image name
//we will create the instance of realImage only if needed
if (realImage == null) {
realImage = new RealImage(fileName);
}
//getting the image from the real image is highly costly
//so we will do this only once
this.name = realImage.getName();
}
return this.name;
}
@Override
public byte[] getData() {
//similar behavior for the data of the image
if (this.data == null) {
if (realImage == null) {
realImage = new RealImage(fileName);
}
//highly costly operation
this.data = realImage.getData();
}
return this.data;
}
}
从而反映了为我们的RealImage
.