代理模式

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 netWork;

    public ProxyServer(NetWork netWork){
        this.netWork = netWork;
    }

    public void check(){
        System.out.println("访问前检查一下网络");
    }

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

 

 

 

 

 

posted @ 2022-08-06 21:21  Mr_sven  阅读(11)  评论(0编辑  收藏  举报