zoukankan      html  css  js  c++  java
  • 一、代理模式:静态代理与动态代理

    静态代理

    • 注:实质就是使代理类和被代理类实现同一个接口,然后在代理类中添加其他操作。

    • MyProxy.java文件

      package cn.qujialin.proxy;
      //代理类
      public class MyProxy implements Operator{
          private Operator operator;
      
          public MyProxy(Operator operator){
              this.operator = operator;
          }
      
          @Override
          public int add(int i, int j) {
              System.out.println("proxy前");
              operator.add(i,j);
              System.out.println("proxy后");
              return 0;
          }
      
          @Override
          public int sub(int i, int j) {
              System.out.println("proxy前");
              operator.sub(i,j);
              System.out.println("proxy后");
              return 0;
          }
      
          @Override
          public int mul(int i, int j) {
              System.out.println("proxy前");
              operator.mul(i,j);
              System.out.println("proxy后");
              return 0;
          }
      
          @Override
          public int div(int i, int j) {
              System.out.println("proxy前");
              operator.div(i,j);
              System.out.println("proxy后");
              return 0;
          }
      }
      //被代理类
      class MyOperator implements Operator{
          @Override
          public int add(int i, int j){
              System.out.println("add......");
              return i + j;
          }
          @Override
          public int sub(int i, int j){
              System.out.println("sub......");
              return i - j;
          }
          @Override
          public int mul(int i, int j){
              System.out.println("mul......");
              return i * j;
          }
          @Override
          public int div(int i, int j){
              System.out.println("div......");
              return i / j;
          }
      
      }
      
      
    • Operato.java

      package cn.qujialin.proxy;
      //接口
      public interface Operator {
          public int add(int i, int j);
          public int sub(int i, int j);
          public int mul(int i, int j);
          public int div(int i, int j);
      }
      
    • TestMain.java

      package cn.qujialin.proxy;
      //测试
      public class TestMain {
          public static void main(String[] args) {
              Operator op = new MyProxy(new MyOperator());
              op.add(1,2);
          }
      }
      

    动态代理

    • MyActiveProxyFactory.java

      package cn.qujialin.activeproxy;
      
      import java.lang.reflect.InvocationHandler;
      import java.lang.reflect.Method;
      import java.lang.reflect.Proxy;
      //代理工厂
      public class MyActiveProxyFactory {
      
          public static Object getProxyInstance(Operator op){
              Object proxy = null;
              ClassLoader cl = op.getClass().getClassLoader();
              Class[] interfaces = op.getClass().getInterfaces();
              InvocationHandler invocationHandler = new InvocationHandler() {
                  @Override
                  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                      String methodName = method.getName();
                      System.out.println("methodName:::" + methodName);
                      Object result = method.invoke(op, args);
                      System.out.println("methodName:::" + methodName);
                      return result;
                  }
              };
              return Proxy.newProxyInstance(cl,interfaces,invocationHandler);
          }
      
      }
      
    • Operator.java

      package cn.qujialin.activeproxy;
      //接口
      public interface Operator {
          public int add(int i, int j);
          public int sub(int i, int j);
          public int mul(int i, int j);
          public int div(int i, int j);
      }
      
    • MyOperator.java

      package cn.qujialin.activeproxy;
      
      import cn.qujialin.activeproxy.Operator;
      
      //被代理类
      public class MyOperator implements Operator{
          @Override
          public int add(int i, int j){
              System.out.println("add......");
              return i + j;
          }
          @Override
          public int sub(int i, int j){
              System.out.println("sub......");
              return i - j;
          }
          @Override
          public int mul(int i, int j){
              System.out.println("mul......");
              return i * j;
          }
          @Override
          public int div(int i, int j){
              System.out.println("div......");
              return i / j;
          }
      
      }
      
      
    • TestMain.java

      package cn.qujialin.activeproxy;
      //测试
      public class TestMain {
          public static void main(String[] args) {
              Operator p = (Operator)MyActiveProxyFactory.getProxyInstance(new MyOperator());
              p.add(3,5);
          }
      }
      
      
    梦还远,路还长!
  • 相关阅读:
    网曝!互联网公司那些老司机才懂的秘密~~
    中国IT行业薪资:与销售相比,程序员真得很“穷”
    太简单了,教你去掉Java代码中烦人的“!=null”
    怎么判断自己在不在一家好公司?
    内部泄露版!互联网大厂的薪资和职级一览
    重磅!GitHub突然宣布,对全球人免费开放全部核心功能
    痛心!Pandownload开发者被抓!我终于决定使用Docker搭建一个多端同步网盘!
    退税:我承认我有赌的成分
    golang实现的简单优先队列
    ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (13)解答
  • 原文地址:https://www.cnblogs.com/qujialin/p/12165932.html
Copyright © 2011-2022 走看看