Proxy Design Pattern in Java

Proxy design pattern

  1. Create a “wrapper” for a remote, or expensive, or sensitive target
  2. Encapsulate the complexity/overhead of the target in the wrapper
  3. The client deals with the wrapper
  4. The wrapper delegates to the target
  5. To support plug-compatibility of wrapper and target, create an interface
// 5. To support plug-compatibility between
// the wrapper and the target, create an interface
interface SocketInterface {
    String readLine();
    void  writeLine(String str);
    void  dispose();
}

class SocketProxy implements SocketInterface {
    // 1. Create a "wrapper" for a remote,
    // or expensive, or sensitive target
    private Socket socket;
    private BufferedReader in;
    private PrintWriter out;

    public SocketProxy(String host, int port, boolean wait) {
        try {
            if (wait) {
                // 2. Encapsulate the complexity/overhead of the target in the wrapper
                ServerSocket server = new ServerSocket(port);
                socket = server.accept();
            } else {
                socket = new Socket(host, port);
            }
            in  = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out = new PrintWriter(socket.getOutputStream(), true);
        } catch(IOException e) {
            e.printStackTrace();
        }
    }

    public String readLine() {
        String str = null;
        try {
            str = in.readLine();
        } catch( IOException e ) {
            e.printStackTrace();
        }
        return str;
    }

    public void writeLine(String str) {
        // 4. The wrapper delegates to the target
        out.println(str);
    }

    public void dispose() {
        try {
            socket.close();
        } catch(IOException e) {
            e.printStackTrace();
        }
    }
}

public class ProxyDemo {
    public static void main( String[] args ) {
        // 3. The client deals with the wrapper
        SocketInterface socket = new SocketProxy( "127.0.0.1", 8080, args[0].equals("first") ? true : false );
        String  str;
        boolean skip = true;
        while (true) {
            if (args[0].equals("second") && skip) {
                skip = !skip;
            } else {
                str = socket.readLine();
                System.out.println("Receive - " + str);
                if (str.equals(null)) {
                    break;
                }
            }
            System.out.print( "Send ---- " );
            str = new Scanner(System.in).nextLine();
            socket.writeLine( str );
            if (str.equals("quit")) {
                break;
            }
        }
        socket.dispose();
    }
}

Output

Receive - GET / HTTP/1.1
Send ----
Receive - Host: localhost:8080
Send ----
Receive - Connection: keep-alive
Send ----
Receive - Cache-Control: max-age=0
Send ----
Receive - Upgrade-Insecure-Requests: 1
Send ----
Receive - User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36
Send ----
Receive - Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Send ----
Receive - DNT: 1
Send ----
Receive - Accept-Encoding: gzip, deflate, sdch, br
Send ----
Receive - Accept-Language: ru,en-US;q=0.8,en;q=0.6,uk;q=0.4
Send ----
Receive - Cookie: JSESSIONID=32E01EAB6C4F723716A8E4A80BC5A381
Send ---- quit

Code examples