冬雨财经 发表于 2024-3-23 14:33:52

通过JDK动态代理类实现一个类中多种方法的不同增强

1.为什么说JDK动态代理必须要实现当前父接口才能使用

  JDK动态代理是基于接口的代理,它要求目标类(被代理的类)必须实现一个或多个接口。这是因为JDK动态代理是通过创建目标类的接口的代理对象来实现的,代理对象实现了目标接口,并在方法调用时委托给InvocationHandler中的invoke方法来处理。
  在JDK动态代理中,Proxy类的newProxyInstance方法接受一个ClassLoader,一组接口和一个InvocationHandler,然后生成一个代理类的实例。这个代理类实例实现了指定的接口,并将方法调用委托给InvocationHandler中的invoke方法。
  自我结论:可能是因为比如生成的$Proxy代理类如果没有父接口 那他内部也不知道通过InvocationHandler来调用谁来进行增强了 还有就是可能有一些决策问题。
2.JDK动态代理的$Proxy实现
  Cat父接口
//父接口
interface Cat {
    //开车
    void drive();
    //下车
    void off();
}  InvocationHandler接口interface InvocationHandler {
    //代理类,代理方法,代理方法参数
    Object invoke(Object proxy, Method method, Object[] args) throws Throwable;
}  $Proxy0代理类class $Proxy0 implements Cat {
    private InvocationHandler invocationHandler;
    private Map<String, Method> methods;

    public $Proxy0(InvocationHandler invocationHandler) {
      this.invocationHandler = invocationHandler;
      this.methods = new HashMap<>();
      try {
            methods.put("drive", Cat.class.getMethod("drive"));
            methods.put("off", Cat.class.getMethod("off"));
      } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
      }
    }

    @Override
    public void drive() {
      try {
            invocationHandler.invoke(this, methods.get("drive"), new Object);
      } catch (Throwable e) {
            throw new RuntimeException(e);
      }
    }

    @Override
    public void off() {
      try {
            invocationHandler.invoke(this, methods.get("off"), new Object);
      } catch (Throwable e) {
            throw new RuntimeException(e);
      }
    }
}  CustomLogicHandler 多个方法不同增强class CustomLogicHandler implements InvocationHandler {
    private Object target;
    private Map<String, Runnable> methodEnhancements;

    public CustomLogicHandler(Object target) {
      this.target = target;
      this.methodEnhancements = new HashMap<>();
      methodEnhancements.put("drive", () -> System.out.println("开车增强"));
      methodEnhancements.put("off", () -> System.out.println("下车增强"));
    }
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
      // 根据方法名获取增强逻辑
      Runnable enhancement = methodEnhancements.get(method.getName());
      if (enhancement != null) {
            enhancement.run();
      }
      // 委托给目标对象执行原方法
      Object result = method.invoke(target, args);
      return result;
    }

  Fute实现类(待增强类)
class FuTe implements Cat {
    @Override
    public void drive() {
      System.out.println("我要开始开车了");
    }

    @Override
    public void off() {
      System.out.println("我要下车了");
    }
    public static void main(String[] args) {
      Cat realObject = new FuTe();
      CustomLogicHandler customLogicHandler = new CustomLogicHandler(realObject);

      Cat proxyObject = (Cat) new $Proxy0(customLogicHandler);

      proxyObject.drive();
      System.out.println("------");
      proxyObject.off();
    }
}  总结:在这个例子中,CustomLogicHandler中的methodEnhancements存储了不同方法的增强逻辑,而不再使用if语句。在$Proxy0中的methods也类似地存储了不同方法的Method对象。这种方式可以更灵活地处理不同方法的增强逻辑,而不需要使用大量的if语句。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: 通过JDK动态代理类实现一个类中多种方法的不同增强