代理模式

 

1
2
3
4
5
6
7
8
9
10
11
12
/**
    * 【代理模式】
    *      特征:
    *          代理类 与 委托类 有相同的接口;
    *          代理类 负责 为委托类 预处理消息、过滤消息、把消息转发给委托类、事后处理消息等;
    *
    *      根据代理类的创建时期,分为2种:
    *          静态代理类:
    *              程序运行前,代理类就已经存在;
    *          动态代理类:
    *              程序运行时,运用反射机制动态创建代理类;
    */

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
    * 【静态代理类】
    *      public interface HelloService{
    *          String echo(String msg);
    *      }
    *
    *      // 委托类
    *      public class HelloServiceImpl implements HelloService{
    *          public String echo(String msg){
    *              return "echo:"+msg;
    *          }
    *      }
    *
    *      // 代理类
    *      public class HelloServiceProxy implements HelloService{
    *          private HelloService helloService;
    *
    *          public HelloServiceProxy(HelloService helloService){
    *              this.helloService=helloService;
    *          }
    *
    *          public String echo(String msg){
    *              System.out.println("before calling echo()");
    *              String result=helloService.echo(msg);
    *              System.out.println("after calling echo()");
    *              return result;
    *          }
    *      }
    *
    *      public static void main(String args[]){
    *          HelloService helloService=new HelloServiceImpl();
    *          HelloService helloServiceProxy=new HelloServiceProxy(helloService);
    *          System.out.println(helloServiceProxy.echo("hello"));
    *      }
    */

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
    * 【动态代理类】
    *      java.lang.reflect包中的 Proxy、InvocationHandler 提供了 生成动态代理类的能力;
    *
    *      public class Proxy implements java.io.Serializable {
    *          // 创建 动态代理类
    *          public static Class<?> getProxyClass(ClassLoader loader, Class<?>... interfaces) throws IllegalArgumentException{}
    *
    *          // 创建 动态代理类的实例
    *          public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h)throws IllegalArgumentException{}
    *      }
    *
    *      public interface InvocationHandler {
    *          public Object invoke(Object proxy, Method method, Object[] args) throws Throwable;
    *      }
    *
    *      【Proxy创建的代理类有如下特点】
    *          1、public、final、非抽象
    *          2、继承 java.lang.reflect.Proxy
    *          3、名称以 $Proxy 开头
    *          4、实现 参数中interfaces 指定所有接口
    *          5、都有一个 public的构造方法,构造方法有一个 InvocationHandler类型的参数
    *          6、Proxy.isProxyClass 判断一个类是否为动态代理类
    *
    *      【Proxy创建的代理实例有如下特点】
    *          1、每个动态代理类实例 都与 一个InvocationHandler实例 关联
    *          2、Proxy.getInvocationHandler 返回与 代理类实例 关联的InvocationHandler实例
    *          3、当程序调用 代理类实例的方法时,会调用 与 代理类实例关联的InvocationHandler实例的 invoke方法
    *
    *
    *      public interface HelloService{
    *          String echo(String msg);
    *      }
    *
    *      // 委托类
    *      public class HelloServiceImpl implements HelloService{
    *          public String echo(String msg){
    *              return "echo:"+msg;
    *          }
    *      }
    *
    *      // 代理工厂
    *      public class HelloServiceProxyFactory {
    *
    *          public static <T> T getHelloServiceProxy(final Object object, final Class<?>... interfaces) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
    *              MyHandler myHandler = new MyHandler(object);
    *
    *              // 第1种方式创建代理类实例
    *              Class<?> proxyClass = Proxy.getProxyClass(object.getClass().getClassLoader(), interfaces);
    *              Constructor<?> constructor = proxyClass.getConstructor(new Class[]{InvocationHandler.class});
    *              T newInstance = (T) constructor.newInstance(myHandler);
    *              return newInstance;
    *
    *              // 第2种方式创建代理类实例
    *              T newProxyInstance = (T) Proxy.newProxyInstance(o.getClass().getClassLoader(), interfaces, myHandler);
    *              return newProxyInstance;
    *          }
    *
    *          static class MyHandler implements InvocationHandler{
    *
    *              private Object object;
    *
    *              public MyHandler(){}
    *
    *              public MyHandler(Object o){
    *                  this.object = o;
    *              }
    *
    *              @Override
    *              public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    *                  System.out.println("before calling "+method);
    *                  Object result=method.invoke(object, args);
    *                  System.out.println("after calling "+method);
    *                  return result;
    *              }
    *
    *          }
    *      }
    *
    *      public static void main(String args[]) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
    *          HelloService helloService=new HelloServiceImpl();
    *          HelloService helloServiceProxy= HelloServiceProxyFactory.getProxyInstance(helloService, HelloService.class);
    *          System.out.println(helloServiceProxy.getClass().getName());
    *          System.out.println(helloServiceProxy.echo("Hello"));
    *      }
    *
    */

  

posted on   anpeiyong  阅读(9)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示