设计模式:代理模式(proxy)

接口的应用:代理模式

代理模式是Java开发中使用较多的一种设计模式。代理设计就是为其他对象提供一种代理以控制对这个对象的访问。

public class ProxyTest {
    
    
    public static void main(String[] args) {
    
    
        Server server = new Server();
        ProxyServer proxyServer = new ProxyServer(server);
        proxyServer.browse();
    }
}

interface Network {
    
    
    void browse();

}

//被代理类
class Server implements Network {
    
    
    @Override
    public void browse() {
    
    
        System.out.println("真实的服务器访问网络");
    }
}

//代理类
class ProxyServer implements Network {
    
    

    private Network work;

    public ProxyServer(Network work) {
    
    
        this.work = work;
    }

    public void check() {
    
    
        System.out.println("联网之前的检查工作");
    }

    @Override
    public void browse() {
    
    
        check();
        work.browse();
    }
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/AmorFati1996/article/details/108727742