1

在以下示例中,来自 wiki 书籍https://en.wikibooks.org/wiki/Computer_Science_Design_Patterns/Proxy

我不确定这比仅创建真实对象并使用其中的显示图像更快/更有效。因为代理无论如何都会在 displayImage 方法中创建真实对象?

//on System B
    class ProxyImage implements Image {

        private RealImage image = null;
        private String filename = null;
        /**
         * Constructor
         * @param FILENAME
         */
        public ProxyImage(final String FILENAME) {
            filename = FILENAME;
        }

        /**
         * Displays the image
         */
        public void displayImage() {
            if (image == null) {
               image = new RealImage(filename);
            }
            image.displayImage();
        }

    }

如果您不使用代理,代理模式肯定不会节省内存,因为它需要实例化两个对象(代理和真实)而不是一个(真实)?

4

2 回答 2

3

从您发布的链接(强调我的):

代理类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.

于 2014-04-25T15:36:37.650 回答
1

这个特定代理的目的似乎是实现所谓的“延迟加载”。它实际上并没有读取文件并在内存中创建图像,直到其他一些代码实际尝试显示它。与将图像放入您从未使用过的内存相比,这可以节省大量时间和内存!

在小例子中很容易想到“我可以只编程聪明而不加载愚蠢的东西。” 但是想象一个更大的系统,你被一个 API 卡住了,它把 aList<Image>作为参数,但实际上只有在用户点击文件名或其他东西时才会绘制一个。这可能是一个显着的推动力。

于 2014-04-25T15:36:28.673 回答