zoukankan      html  css  js  c++  java
  • java 动态代理

    代理是基本的设计模式之一它为你提供额外的或不同的操作,而插入的用来代替"实际"对象的对象.

    package typeinfo;
    //: typeinfo/SimpleProxyDemo.java
    import static net.mindview.util.Print.*;
    
    interface Interface {
      void doSomething();
      void somethingElse(String arg);
    }
    
    class RealObject implements Interface {
      public void doSomething() { print("doSomething"); }
      public void somethingElse(String arg) {
        print("somethingElse " + arg);
      }
    }    
    
    class SimpleProxy implements Interface {
      private Interface proxied;
      public SimpleProxy(Interface proxied) {
        this.proxied = proxied;
      }
      public void doSomething() {
        print("SimpleProxy doSomething");
        proxied.doSomething();
      }
      public void somethingElse(String arg) {
        print("SimpleProxy somethingElse " + arg);
        proxied.somethingElse(arg);
      }
    }    
    
    class SimpleProxyDemo {
      public static void consumer(Interface iface) {
        iface.doSomething();
        iface.somethingElse("bonobo");
      }
      public static void main(String[] args) {
        consumer(new RealObject());
        consumer(new SimpleProxy(new RealObject()));
      }
    } /* Output:
    doSomething
    somethingElse bonobo
    SimpleProxy doSomething
    doSomething
    SimpleProxy somethingElse bonobo
    somethingElse bonobo
    *///:~

    动态代理的思想更向前迈进了一步,因为它可以动态地创建代理并动态地处理对所代理方法的调用.

    package typeinfo;
    //: typeinfo/SimpleDynamicProxy.java
    import java.lang.reflect.*;
    
    class DynamicProxyHandler implements InvocationHandler {
      private Object proxied;
      public DynamicProxyHandler(Object proxied) {
        this.proxied = proxied;
      }
      public Object
      invoke(Object proxy, Method method, Object[] args)
      throws Throwable {
        System.out.println("**** proxy: " + proxy.getClass() +
          ", method: " + method + ", args: " + args);
        if(args != null)
          for(Object arg : args)
            System.out.println("  " + arg);
        return method.invoke(proxied, args);
      }
    }    
    
    class SimpleDynamicProxy {
      public static void consumer(Interface iface) {
        iface.doSomething();
        iface.somethingElse("bonobo");
      }
      public static void main(String[] args) {
        RealObject real = new RealObject();
        consumer(real);
        // Insert a proxy and call again:
        Interface proxy = (Interface)Proxy.newProxyInstance(
          Interface.class.getClassLoader(),
          new Class[]{ Interface.class },
          new DynamicProxyHandler(real));
        consumer(proxy);
      }
    } /* Output: (95% match)    
    doSomething
    somethingElse bonobo
    **** proxy: class $Proxy0, method: public abstract void Interface.doSomething(), args: null
    doSomething
    **** proxy: class $Proxy0, method: public abstract void Interface.somethingElse(java.lang.String), args: [Ljava.lang.Object;@42e816
      bonobo
    somethingElse bonobo
    *///:~
  • 相关阅读:
    Struts2声明式异常处理
    几种常用的过滤器
    Jdk 和 Tomcat的 安装。(旧版本,请看新版本3篇)
    java 判断字符串是否相等
    PreparedStatement 查询 In 语句 setArray 等介绍。
    String、StringBuffer与StringBuilder之间区别
    IntelliJ IDEA 里 查看一个函数注释的方法是 ctrl+q
    Java字符串拼接效率对比
    Java 中判断字符串是否为空
    IntelliJ IDEA + Tomcat ;On Upate Action 与 On Frame Deactivation
  • 原文地址:https://www.cnblogs.com/jiangfeilong/p/10354968.html
Copyright © 2011-2022 走看看