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

    自定义2个接口subject1,subject2

     1 package proxy;
     2 
     3 public interface Subject1 {
     4     public void doSomething();
     5 }
     6 
     7 
     8 
     9 package proxy;
    10 
    11 public interface Subject2 {
    12     public void doOtherthing();
    13 }

    定义2个真实的RealSubject1(implements subject1),RealSubject2(implements subject1,subject2)

     1 package proxy;
     2 
     3 public class RealSubject1 implements Subject1 {
     4 
     5     @Override
     6     public void doSomething() {
     7         // TODO Auto-generated method stub
     8         System.out.println("realSubject1 invoke from Subject1");
     9     }
    10 
    11 }
    12 
    13 package proxy;
    14 
    15 public class RealSubject2 implements Subject2,Subject1{
    16 
    17     @Override
    18     public void doOtherthing() {
    19         System.out.println("realSubject2 invoke from Subject2");
    20         
    21     }
    22     @Override
    23     public void doSomething() {
    24         System.out.println("realSubject2 invoke from Subject1");
    25 
    26     }
    27 
    28 }

    创建一个invocationHandler

     1 package proxy;
     2 
     3 import java.lang.reflect.InvocationHandler;
     4 import java.lang.reflect.Method;
     5 
     6 public class DynanicSubject implements InvocationHandler {
     7     private Object o1;
     8     public DynanicSubject(Object obj){
     9         this.o1=obj;
    10     }
    11     @Override
    12     public Object invoke(Object proxy, Method method, Object[] args)
    13             throws Throwable {
    14         System.out.println("执行前");
    15         method.invoke(o1, args);
    16         System.out.println("执行后");
    17         return null;
    18     }
    19 
    20 }

    创建测试类

     1 package proxy;
     2 
     3 import java.lang.reflect.InvocationHandler;
     4 import java.lang.reflect.Proxy;
     5 
     6 public class Client {
     7     public static void main(String[] args) {
     8                //分别创建真实类对象以及handler
     9         Subject1 sub1=new RealSubject1();
    10         RealSubject2 sub2=new RealSubject2();
    11                  
    12         InvocationHandler hander=new DynanicSubject(sub1);
    13         InvocationHandler hander2=new DynanicSubject(sub2);
    14                  //创建动态代理类 (由于RealSubject2实现2个接口,因此下面进行2此转换,分别调用2个方法)
    15         Subject1 su=(Subject1)Proxy.newProxyInstance(hander.getClass().getClassLoader(), sub1.getClass().getInterfaces(), hander);
    16         Subject2 su2=(Subject2)Proxy.newProxyInstance(hander2.getClass().getClassLoader(), sub2.getClass().getInterfaces(), hander2);
    17         su.doSomething();
    18         su2.doOtherthing();
    19         Subject1 su3=(Subject1)Proxy.newProxyInstance(hander2.getClass().getClassLoader(), sub2.getClass().getInterfaces(), hander2);
    20         su3.doSomething();
    21     }
    22 }  

    执行结果:

    执行前
    realSubject1 invoke from Subject1
    执行后
    执行前
    realSubject2 invoke from Subject2
    执行后
    执行前
    realSubject2 invoke from Subject1
    执行后

  • 相关阅读:
    leetcode Remove Linked List Elements
    leetcode Word Pattern
    leetcode Isomorphic Strings
    leetcode Valid Parentheses
    leetcode Remove Nth Node From End of List
    leetcode Contains Duplicate II
    leetcode Rectangle Area
    leetcode Length of Last Word
    leetcode Valid Sudoku
    leetcode Reverse Bits
  • 原文地址:https://www.cnblogs.com/oldcownotGiveup/p/5366323.html
Copyright © 2011-2022 走看看