concept

  • Proxying and proxied objects are defined before proxiing, and they both implement the same interface or inherit from the same abstract class

  • Static proxy implementation can be divided into inheritance implementation and aggregation implementation

Code implementation

The business requirements

Service requirements Interfaces for existing HTTP API requests: 1. Logs the URL and result of the request. 2

  • HttpApiThe interface definition
    package cn.tswine.dp.proxy;
    
    / * * *@Author: silly
     * @Date: 2020/1/31 fell *@Version 1.0
     * @Desc* /
    public interface HttpApi {
        String request(String url);
    }
    Copy the code
  • RealHttpApiBusiness implementation class
    public class RealHttpApi implements HttpApi {
       @Override
       public String request(String url) {
           try {
               // Simulate the request time
               Thread.sleep(new Random().nextInt(1000));
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
           // The pattern returns the result
           return "success"; }}Copy the code

Inheritance implementation

  • LogHttpApiProxy
    /** * logging agent */
    public class LogHttpApiProxy extends RealHttpApi {
        @Override
        public String request(String url) {
            System.out.println("request url:" + url);
            String response = super.request(url);
            System.out.println("response:" + response);
            returnresponse; }}Copy the code
  • TimeHttpApiProxy
    /** * time logging agent: Records the time of the request */
    public class TimeHttpApiProxy extends LogHttpApiProxy {
    
        @Override
        public String request(String url) {
            long start = System.currentTimeMillis();
            String response = super.request(url);
            long end = System.currentTimeMillis();
            System.out.println("request time:" + (end - start));
            returnresponse; }}Copy the code
  • Client
    /** * client */
    public class Client {
    
        public static void main(String[] args) {
            String url = "https://www.csdn.net";
            HttpApi httpApi = newTimeHttpApiProxy(); httpApi.request(url); }}Copy the code
  • The execution result
    request url:https://www.csdn.net
    response:success
    request time:629
    Copy the code

Aggregation implementation

  • LogHttpApiProxy
    /** * logging agent */
    public class LogHttpApiProxy implements HttpApi {
    
        HttpApi api;
    
        public LogHttpApiProxy(HttpApi api) {
            this.api = api;
        }
    
        @Override
        public String request(String url) {
            System.out.println("request url:" + url);
            String response = api.request(url);
            System.out.println("response:" + response);
            returnresponse; }}Copy the code
  • TimeHttpApiProxy
    /** * time logging agent: Records the time of the request */
    public class TimeHttpApiProxy implements HttpApi {
    
        HttpApi api;
    
        public TimeHttpApiProxy(HttpApi api) {
            this.api = api;
        }
    
        @Override
        public String request(String url) {
            long start = System.currentTimeMillis();
            String response = api.request(url);
            long end = System.currentTimeMillis();
            System.out.println("request time:" + (end - start));
            returnresponse; }}Copy the code
  • Client
    /** * client */
    public class Client {
    
        public static void main(String[] args) {
            String url = "https://www.csdn.net";
            HttpApi realHttpApi =new RealHttpApi();
            HttpApi logProxy = new LogHttpApiProxy(realHttpApi);
            HttpApi timeProxy = newTimeHttpApiProxy(logProxy); timeProxy.request(url); }}Copy the code
  • The execution result
    request url:https://www.csdn.net
    response:success
    request time:315
    Copy the code

Inheritance and aggregation implement summary

  • Inheritance: Each additional logic, one more proxy class; Logical order changes order once, one more proxy class
  • Aggregation: Each additional logic, an additional proxy class; The logical order changes the order once, only after the call changes the order logic, without requiring an additional proxy class
  • Whether inherited or aggregated, the resulting proxied object is not the same object as the proxied object
  • The static proxy pattern is better suited for aggregation than inheritance