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。

  • 相关阅读:
    多重共性和VIF检验
    类和对象
    哈希桶
    第9章 硬件抽象层:HAL
    第10章 嵌入式Linux的调试技术
    第8章 让开发板发出声音:蜂鸣器驱动
    第7章 LED将为我闪烁:控制发光二极管
    第6章 第一个Linux驱动程序:统计单词个数
    第5章 搭建S3C6410开发板的测试环境
    第四章:源代码的下载与编译
  • 原文地址:https://www.cnblogs.com/chayu3/p/3272589.html
Copyright © 2011-2022 走看看