Static proxy in proxy mode

Proxy mode:

Provides a proxy for other objects to control access to this object. In some cases, an object is inappropriate or cannot directly reference another object,

The proxy object can act as an intermediary between the client and the target object.

For example: proxy server, product agency, industrial and commercial agency, accounting agency, real estate agency, occupation agency, etc.

 

composition:

Abstract roles: Business methods that declare real role implementations through interfaces or abstract classes.

Proxy role: It implements an abstract role and is the proxy of the real role. It implements the abstract method through the business logic method of the real role, and can add its own operations.

Real role: Implement abstract roles, define the business logic to be implemented by real roles, and be invoked by proxy roles.

 

Here is example code for a proxy server that implements proxy mode:

Abstract role:

/**
 * server
 * Created by jiangzhq on 2015/6/23.
 */
public interface Server {
    public void service();
}

 

Real Persona: Provide real service

/**
 * External network server, providing access to overseas services
 * Created by jiangzhq on 2015/6/23.
 */
public class InternetServer implements Server{

    public void service() {
        System.out.println("External network server, providing access to overseas services");
    }
}

 

Proxy role: To perform actions in place of the real role, you need to inject the real role. There are multiple methods to inject: constructor, set method, etc.

/**
 * Proxy servers, in addition to providing overseas website services, also need to break through the GFW blockade
 * Created by jiangzhq on 2015/6/23.
 */
public class ProxyServer implements Server {

    InternetServer server;
    // inject real character
    public ProxyServer() {
        server = new InternetServer();
    }

    public void service() {
        before();
        server.service();
        after();
    }

    public void before(){
        System.out.println("Break GFW blockade");
    }

    public void after(){
        System.out.println("It is a wise choice to use a proxy server to access overseas services");
    }

}

 

/**
 * Client test class
 * Created by jiangzhq on 2015/6/23.
 */
public class ProxyTest {

    public static void main(String[] args) {
        Server proxyServer = new ProxyServer();
        proxyServer.service();
    }
}

 

The design of the proxy pattern is to use abstract classes or interfaces as "abstract roles", and each "proxy role" represents a "real role".

Using the proxy mode, you can add additional functions without changing the original class. The code is pollution-free, low-coupling, and conforms to the open-closed principle.

This code implementation method in this article is a static proxy mode. Although it does not pollute the original code, if the number of proxy classes is large, a lot of proxy class code will need to be written, which will inevitably cause the system to be bloated. The use of dynamic proxy can solve this problem. , and see the dynamic proxy of proxy mode in the next article   .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326339358&siteId=291194637