设计模式之代理模式-静态代理

143 阅读2分钟

概念

  • 代理和被代理对象在代理之前是确定的,他们都实现相同的接口或者继承相同的抽象类 在这里插入图片描述
  • 静态代理实现可分为:继承方式实现和聚合方式实现

代码实现

业务需求

  业务要求对已有的http api请求的接口:1、实现对请求的url和请求结果记录日志 2、记录请求执行的时间

  • HttpApi 接口定义
    package cn.tswine.dp.proxy;
    
    /**
     * @Author: silly
     * @Date: 2020/1/31 20:44
     * @Version 1.0
     * @Desc
     */
    public interface HttpApi {
        String request(String url);
    }
    
  • RealHttpApi业务实现类
    public class RealHttpApi implements HttpApi {
       @Override
       public String request(String url) {
           try {
               //模拟请求的时间
               Thread.sleep(new Random().nextInt(1000));
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
           //模式返回结果
           return "success";
       }
    }
    

继承方式实现

  • LogHttpApiProxy
    /**
     * 日志记录代理
     */
    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);
            return response;
        }
    }
    
  • TimeHttpApiProxy
    /**
     * 时间记录代理:记录请求的时间
     */
    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));
            return response;
        }
    }
    
  • Client
    /**
     * 客户端
     */
    public class Client {
    
        public static void main(String[] args) {
            String url = "https://www.csdn.net";
            HttpApi httpApi = new TimeHttpApiProxy();
            httpApi.request(url);
        }
    }
    
  • 执行结果
    request url:https://www.csdn.net
    response:success
    request time:629
    

聚合方式实现

  • LogHttpApiProxy
    /**
     * 日志记录代理
     */
    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);
            return response;
        }
    }
    
  • TimeHttpApiProxy
    /**
     * 时间记录代理:记录请求的时间
     */
    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));
            return response;
        }
    }
    
  • 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 = new TimeHttpApiProxy(logProxy);
            timeProxy.request(url);
        }
    }
    
  • 执行结果
    request url:https://www.csdn.net
    response:success
    request time:315
    

继承和聚合方式实现总结

  • 继承方式:每多一个逻辑,多一个代理类;逻辑顺序改变一次顺序,多一个代理类
  • 聚合方式:每多一个逻辑,多一个代理类;逻辑顺序改变一次顺序,只需在调用改变顺序逻辑,无需多一个代理类
  • 无论是继承方式还是聚合方式,得到的代理类对象与被代理类对象不是同一个对象
  • 聚合比继承方式更适合静态代理模式