我试图理解代理设计模式。但我无法理解代理设计模式的用法。我从维基百科获得了这个代码示例
interface Image {
public void displayImage();
}
//on System A
class RealImage implements Image {
private String filename = null;
/**
* Constructor
* @param filename
*/
public RealImage(final String filename) {
this.filename = filename;
loadImageFromDisk();
}
/**
* Loads the image from the disk
*/
private void loadImageFromDisk() {
System.out.println("Loading " + filename);
}
/**
* Displays the image
*/
public void displayImage() {
System.out.println("Displaying " + filename);
}
}
//on System B
class ProxyImage implements Image {
private RealImage image = null;
private String filename = null;
/**
* Constructor
* @param filename
*/
public ProxyImage(final String filename) {
this.filename = filename;
}
/**
* Displays the image
*/
public void displayImage() {
if (image == null) {
image = new RealImage(filename);
}
image.displayImage();
}
}
class ProxyExample {
/**
* Test method
*/
public static void main(String[] args) {
final Image IMAGE1 = new ProxyImage("HiRes_10MB_Photo1");
final Image IMAGE2 = new ProxyImage("HiRes_10MB_Photo2");
IMAGE1.displayImage(); // loading necessary
IMAGE1.displayImage(); // loading unnecessary
IMAGE2.displayImage(); // loading necessary
IMAGE2.displayImage(); // loading unnecessary
IMAGE1.displayImage(); // loading unnecessary
}
}
在这个例子中,他们说第二次dispalyImage不需要加载。甚至可以直接访问RealImage对象。
final Image IMAGE1 = new RealImage("HiRes_10MB_Photo1");
final Image IMAGE2 = new RealImage("HiRes_10MB_Photo2");
IMAGE1.displayImage(); // loading necessary
IMAGE1.displayImage(); // loading unnecessary
IMAGE2.displayImage(); // loading necessary
IMAGE2.displayImage(); // loading unnecessary
IMAGE1.displayImage(); // loading unnecessary
我需要了解此模式中ProxyImage类的用法。
答案 0 :(得分:9)
你知道,我同意你的看法。我觉得他们可以用于代理模式的很多更好的例子。 This seems to use the same example but it's explained much better.你应该看一下。
基本上,这一切都归结为这个评论:
// create the Image Object only when the image is required to be shown
这是proxy
在此示例中为您提供的好处。如果您不显示图像,则不需要支付加载图像的罚款:
package proxy;
/**
* 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
}
}
答案 1 :(得分:2)
代理表示“代替”,代表'或代表他人的权限,或者可用于表示某事物价值的数字。 代理设计模式也称为代理,句柄和包装器。
当我们想要创建一个包装器来覆盖客户端主要对象的复杂性时使用它。
代理设计模式的一些真实示例:
1)银行的heque或信用卡是我们银行账户中的内容的代理。它可以用来代替现金,并提供在需要时获取现金的方法。而这正是代理模式所做的 - “控制和管理对他们正在保护的对象的访问”。
2)过去拥有代理的公司或公司限制了几个站点访问。代理首先检查您要连接的主机,如果它不是受限站点列表的一部分,则它连接到真正的互联网。