Design mode: proxy mode (proxy)

Interface application: proxy mode

The proxy mode is a design mode that is frequently used in Java development. Proxy design is to provide a proxy for other objects to control access to this object.

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

Insert picture description here

Guess you like

Origin blog.csdn.net/AmorFati1996/article/details/108727742