zl程序教程

您现在的位置是:首页 >  云平台

当前栏目

代理模式

2023-09-27 14:27:33 时间

一个类代表另一个类的功能。

意图:

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

主要解决:

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

何时使用:

想在访问一个类时做一些控制。

如何解决:

增加中间层。

关键代码:

实现与被代理类组合。

优点:

1、职责清晰。

2、高扩展性。

3、智能化。

缺点:

1、由于在客户端和真实主题之间增加了代理对象,因此有些类型的代理模式可能会造成请求的处理速度变慢。

2、实现代理模式需要额外的工作,有些代理模式的实现非常复杂。

使用场景:按职责来划分,通常有以下使用场景:

1、远程代理。

2、虚拟代理。

3、Copy-on-Write 代理。

4、保护(Protect or Access)代理。

5、Cache代理。

6、防火墙(Firewall)代理。

7、同步化(Synchronization)代理。

8、智能引用(Smart Reference)代理。

注意事项:

1、和适配器模式的区别:适配器模式主要改变所考虑对象的接口,而代理模式不能改变所代理类的接口。

2、和装饰器模式的区别:装饰器模式为了增强功能,而代理模式是为了加以控制。

//创建一个接口。
public interface Image {
    void display();
}

//创建实现接口的实体类。
public class RealImage implements Image {
    private String file;

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

    private void loadFromDisk(String file) {
        System.out.println("从磁盘加载文件");
    }

    @Override
    public void display() {
        System.out.println("展示图片");
    }
}

public class ProxyImage implements Image {
    private String file;
    private RealImage realImage;

    public ProxyImage(String file) {
        this.file = file;
    }

    @Override
    public void display() {
        if(realImage==null){
            realImage=new RealImage(file);
        }
        realImage.display();
    }
}

//当被请求时,使用 ProxyImage 来获取 RealImage 类的对象。
public class ProxyTest {
    public static void main(String[] args) {
        Image image=new ProxyImage("");
        image.display();
    }
}