代理模式 The Proxy Pattern
来源:互联网 发布:win10安装ubuntu双系统 编辑:程序博客网 时间:2024/05/16 09:24
代理模式
为另一个对象提供一个替身或占位符以控制对这个对象的访问。
1、使用代理模式创建代表(representative)对象,让代表对象控制某个对象的访问,被代理的对象可以是远程的对象,创建开销大的对象或需要安全控制的对象。
类图
1、Subject:Proxy和RealSubject都实现此接口,客户可以像处理RealSubject一样处理Proxy对象。
2、Proxy:持有Subject类型的引用(指向RealSubject)。Proxy会控制对RealSubject的访问。
代理模式在结构上类似装饰者,但是目的不同,装饰者为对象加上行为,而代理则是控制访问。
(一)远程代理(RMI过程)
(二)虚拟代理示例
1、创建了一个用来显示的ImageProxy,页面的请求将由Proxy来处理。
2、在某个时间点,图像被返回,ImageIcon被完整实例化。
3、下次调用到painIcon() 时,代理就委托给ImageIcon
(三)保护代理(用Java的动态代理实现)
在java.lang.reflect包中有自己的代理支持,利用这个包你可以在运行时动态的创建一个代理类,实现一个或多个接口,并将方法的调用转发到你所指定的类。即java的动态代理技术。
1、proxy上的任何方法调用都会被传入InvocationHandler类,InvocationHandler控制对RealSubject的访问。
如下程序:
(1)proxy.say( "tom", 10);
(2)MyInvocationHandler的invoke(Object proxy, Method method, Object[] args)会被调用。
(3)invoke() 方法里设置如何处理请求:可以做一些预处理;可以转发给RealSubject;也可以做其他处理。
程序
import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;interface Subject { public String say(String name, int age);}class RealSubject implements Subject { public String say(String name, int age) { return "Hi "+name + ", You have been " + age+" years old." ; }}class MyInvocationHandler implements InvocationHandler { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { for ( int i = 0; i < args. length; i++) { if (String. class.isInstance(args[i])) { String temp = (String) args[i]; args[i] = temp.toUpperCase(); } } Subject realObj= new RealSubject(); Object temp = method.invoke(realObj, args); return temp; }}public class InvocationHandlerTest { public static void main(String[] args) { Subject proxy = (Subject)Proxy.newProxyInstance( Subject. class.getClassLoader(), new Class[]{Subject. class}, new MyInvocationHandler() ); String info = proxy.say("tom", 10); System. out.println(info); }}
输出:
Hi TOM, You have been 10 years old.
(转载请注明出处 ^.^)
<script type="text/javascript"><!--google_ad_client = "ca-pub-1944176156128447";/* cnblogs 首页横幅 */google_ad_slot = "5419468456";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
- 代理模式 The Proxy Pattern
- 代理模式(Proxy Pattern)
- 代理模式(Proxy Pattern)
- Proxy Pattern (代理模式)
- Proxy pattern 代理模式
- 代理模式(Proxy pattern)
- 代理模式 Proxy Pattern
- 代理模式【Proxy Pattern】
- 代理模式(Proxy Pattern)
- 代理模式(PROXY PATTERN)
- 代理模式(Proxy Pattern)
- 代理模式(Proxy Pattern)
- 代理模式【Proxy Pattern】
- 代理模式【Proxy Pattern】
- proxy pattern 代理模式
- Proxy Pattern 代理模式
- 代理模式-Proxy Pattern
- 代理模式 Proxy Pattern
- VC中获取窗口句柄的各种方法 .
- HDU-1864 EXCEL排序
- 语言 unsigned 和 signed 类型相互转换深入理解
- oracle 简单分区的学习
- C# Math常用的方法
- 代理模式 The Proxy Pattern
- 如何编写更佳的bug report
- C# 4.0的并行任务
- ubuntu下安装rpm
- C++ - assert()函数
- HTTP长连接小议
- hdu 1232 畅通工程(最小生成树or并查集)
- QT 学习笔记 窗口控件的布局
- Android 混编 防止 反编译