首页 > 技术文章 > 设计模式-动态代理

caoqh 2019-07-23 15:03 原文

 

 1 package com.demo.basis.reflect;
 2 import java.lang.reflect.*;
 3 interface Person
 4 {
 5     void walk();
 6     void sayHello(String name);
 7 }
 8 /**
 9  * 动态代理
10  * @author boco
11  *
12  */
13 class MyInvokationHandler implements InvocationHandler
14 {
15     /*
16     执行动态代理对象的所有方法时,都会被替换成执行如下的invoke方法
17     其中:
18     proxy:代表动态代理对象
19     method:代表正在执行的方法
20     args:代表调用目标方法时传入的实参。
21     */
22     public Object invoke(Object proxy, Method method, Object[] args)
23     {
24         System.out.println("----正在执行的方法:" + method);
25         if (args != null)
26         {
27             System.out.println("下面是执行该方法时传入的实参为:");
28             for (Object val : args)
29             {
30                 System.out.println(val);
31             }
32         }
33         else
34         {
35             System.out.println("调用该方法没有实参!");
36         }
37         return null;
38     }
39 }
40 public class ProxyTest
41 {
42     public static void main(String[] args) throws Exception
43     {
44         // 创建一个InvocationHandler对象
45         InvocationHandler handler = new MyInvokationHandler();
46         //使用Proxy生成一个动态代理类proxyClass
47         Class proxyClass = Proxy.getProxyClass(Person.class.getClassLoader(), new Class[]{Person.class});
48         //获取proxyClass中夹带一个参数InvocationHandler的构造器
49         Constructor ctor = proxyClass.getConstructor(new Class[]{InvocationHandler.class});
50         Person p = (Person)ctor.newInstance(new Object[]{handler});
51 //        // 使用指定的InvocationHandler来生成一个动态代理对象
52 //        Person p = (Person)Proxy.newProxyInstance(Person.class.getClassLoader(), new Class[]{Person.class}, handler);
53 //        // 调用动态代理对象的walk()和sayHello()方法
54         p.walk();
55         p.sayHello("孙悟空");
56     }
57 }

 

 

public interface Dog {
    public void info();
    public void run();
}
 1 public class GunDog implements Dog {
 2     public void info() {
 4         System.out.println("我是一只猎狗");
 5     }
 6     public void run() {
 8         System.out.println("我奔跑急速");
 9     }
10 }
1 public class DogUtil {
2     public void method1(){
3         System.out.println("第一个方法");
4     }
5     public void method2(){
6         System.out.println("第二个方法");
7     }
8 }
 1 public class MyInvokationHander implements InvocationHandler {
 2     private Object target;
 3     public Object invoke(Object obj, Method method, Object[] aobj)throws Throwable {
 4         DogUtil du = new DogUtil();
 5         du.method1();
 6         Object result = method.invoke(target, aobj);
 7         du.method2();
 8         return result;
 9     }
10     public Object getTarget() {
11         return target;
12     }
13     public void setTarget(Object target) {
14         this.target = target;
15     }
16 }
1 public class MyProxyFactory {
2     public static Object getPorxy(Object target){
3         MyInvokationHander handler = new MyInvokationHander();
4         handler.setTarget(target);
5         return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), handler);
6     }
7 }
1 public class ProxyDemo {
2     public static void main(String[] args) {
4         Dog target = new GunDog();
5         Dog dog = (Dog) MyProxyFactory.getPorxy(target);
6         dog.info();
7         dog.run();
8     }
9 }

 

推荐阅读