zoukankan      html  css  js  c++  java
  • Java Proxy

    Client---->Interface A

        --        --

            代理类     Class AImpl

    代理类是动态生成的,借助Proxy类和InvocationHandler接口进行实现,InvocationHandler的invoke函数就是调用目标类实现的功能接口的地方,可以在这里进行访问控制,添加额外的处理逻辑。

    使用方法:1,通过实现InvocationHandler接口创建自己的调用处理器。2,通过Proxy类的newProxyInstance函数创建动态代理类。

    Example:

    public interface Chatroom{

      abstract public void createChatroom();

    }

    public class ChatroomImpl{

      public void createChatroom() {

        System.out.println("Chatroom is created!");

      }

    }

    public class ChatInvocationHandler implements InvocationHandler{

      private static int count=0;

      private Object target;

      public ChatInvocationHandler(Object target){

        this.target=target;

      }

      public void setTarget(Object target){

        this.target=target;

      }

      public Object invoke(Object proxy, Method method, Object[] args) throws Throwable{

        if(count++%2==0){

          return method.invoke(target, args);  //Control in what circumtances invoke the method of ChatroomImpl.

        }

        System.out.println("Cannot create chatroom!");

        return null;

      }

    }

    主函数进行调用:

    ChatInvocationHandler chatHandler = new ChatInvocationHandler(new ChatroomImpl());

    Chatroom proxy = (Chatroom)Proxy.newProxyInstance(Chatroom.class.getClassLoader(), new Class[]{Chatroom.class}, chatHandler);

    for(int i=0; i<10; i++){

      if(proxy!=null)

        proxy.createChatroom();

    }

    这个proxy object就如同implement了Chatroom interface,在call createChatroom()函数的时候,是走的chatHandler的invoke函数,然后再逻辑性地调用原本目标类ChatroomImpl的createChatroom()函数。

    In my old project, there is the LFIXBuilderHandler which implements InvocationHandler,在创建proxy的时候Proxy.newProxyInstance(LFIXProxy.class.getClassLoader(), classList(type=LFIXBuilder), handler)。这里LFIXBuilder是generate出来的interface,含有无数的fix field method with tag annotation,如果你创建impl class来set 你的message会非常麻烦,所以通过proxy的invoke(Object proxy, Method method, Object[] args)方法,可以统一地定义如何set message fields。

  • 相关阅读:
    git常用命令图解 & 常见错误
    关于ES6的Promise的使用深入理解
    几种简单排序算法代码
    HttpMessageNotWritableException异常解决办法
    关于 java swing 使用按钮关闭窗口
    几种简单的排序总结
    MJ老师博客园的美化代码
    [转]iOS开发之常用的数学函数和常数
    iOS如何简单实现绘制爱心?
    重新入门python爬虫到放弃
  • 原文地址:https://www.cnblogs.com/chayu3/p/3272589.html
Copyright © 2011-2022 走看看