> For the complete documentation index, see [llms.txt](https://lewiszlw.gitbook.io/notebooks/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lewiszlw.gitbook.io/notebooks/blogs/history/she-ji-mo-shi-zhi-dai-li-mo-shi.md).

# 设计模式之代理模式

在代理模式（Proxy Pattern）中，一个类代表另一个类的功能。这种类型的设计模式属于结构型模式。

## 介绍

在代理模式中，我们创建具有现有对象的对象，以便向外界提供功能接口。

**意图**：为其他对象提供一种代理以控制对这个对象的访问。

**主要解决**：在直接访问对象时带来的问题，比如说：要访问的对象在远程的机器上。在面向对象系统中，有些对象由于某些原因（比如对象创建开销很大，或者某些操作需要安全控制，或者需要进程外的访问），直接访问会给使用者或者系统结构带来很多麻烦，我们可以在访问此对象时加上一个对此对象的访问层。

**何时使用**：想在访问一个类时做一些控制。

**如何解决**：增加中间层。

**关键代码**：实现与被代理类组合。

## Demo

1.Image接口

```java
package com.zlw.demo;

public interface Image {
    void display();
}
```

2.被代理的类RealImage

```java
package com.zlw.demo;

public class RealImage implements Image{

    private String filename;

    public RealImage(String filename) {
        this.filename = filename;
        loadFromDisk(filename);
    }

    @Override
    public void display() {
        System.out.println("Displaying: " + filename);
    }

    private void loadFromDisk(String filename) {
        System.out.println("Loading: " + filename);
    }
}
```

3.代理类ProxyImage

```java
package com.zlw.demo;

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();
    }
}
```

4.测试用例

```java
package com.zlw.demo;

public class ProxyPatternDemo {
    public static void main(String[] args) {
        Image image = new ProxyImage("image001.jpg");
        image.display();
    }
}
```

**理解**：使用代理类ProxyImage来控制真实类RealImage的行为。当我们想要调用RealImage类的display行为，使用RealImage类的唯一标示（即filename）来创建属于这个RealImage的ProxyImage，然后利用ProxyImage的display行为来间接调用RealImage的display行为。

## 参考资料

<http://www.runoob.com/design-pattern/proxy-pattern.html>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lewiszlw.gitbook.io/notebooks/blogs/history/she-ji-mo-shi-zhi-dai-li-mo-shi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
