6.5 代理

来源:互联网 发布:用户购买数据分析 编辑:程序博客网 时间:2024/06/07 11:52

1 什么时候使用代理

运行时才能确定类要实现哪个接口,这样的情况使用代理。

Object invoke(Object proxy, Method method, Object[] args)

2 创建代理对象

import java.lang.reflect.*;import java.util.*;/** * This program demonstrates the use of proxies. * @version 1.00 2000-04-13 * @author Cay Horstmann */public class ProxyTest{   public static void main(String[] args)   {      Object[] elements = new Object[1000];      // fill elements with proxies for the integers 1 ... 1000      for (int i = 0; i < elements.length; i++)      {         Integer value = i + 1;         InvocationHandler handler = new TraceHandler(value);         Object proxy = Proxy.newProxyInstance(null, new Class[] { Comparable.class } , handler);         elements[i] = proxy;      }      // construct a random integer      Integer key = new Random().nextInt(elements.length) + 1;      // search for the key      int result = Arrays.binarySearch(elements, key);      // print match if found      if (result >= 0) System.out.println(elements[result]);   }}/** * An invocation handler that prints out the method name and parameters, then * invokes the original method */class TraceHandler implements InvocationHandler{   private Object target;   /**    * Constructs a TraceHandler    * @param t the implicit parameter of the method call    */   public TraceHandler(Object t)   {      target = t;   }   public Object invoke(Object proxy, Method m, Object[] args) throws Throwable   {      // print implicit argument      System.out.print(target);      // print method name      System.out.print("." + m.getName() + "(");      // print explicit arguments      if (args != null)      {         for (int i = 0; i < args.length; i++)         {            System.out.print(args[i]);            if (i < args.length - 1) System.out.print(", ");         }      }      System.out.println(")");      // invoke actual method      return m.invoke(target, args);   }}

3 代理类的特性

真心不知道要怎么用!!!

0 0
原创粉丝点击