设计模式:代理模式与装饰模式
2016-08-02 18:18
344 查看
1、装饰者模式与代理模式 (静态代理)
在日常开发里面,我们经常需要给某个类的方法增加加某些特定的功能。
例如:有婴儿,婴儿会吃饭和走动,如以下类
package com.scl.designpattern.proxy.dynamic.cglib; //客户端 public class Client2 { public static void main(String[] args) { HealthHandle h = new HealthHandle(); Child child = (Child) h.getProxyInstance(new Child()); child.eat(); child.breath(); } }客户端
源码Interceptor签名如下
/** * All generated proxied methods call this method instead of the original method. * The original method may either be invoked by normal reflection using the Method object, * or by using the MethodProxy (faster). 官方建议使用MethodProxy对方法进行调用 * @param obj "this", the enhanced object 跟接口代理一样,第一个参数为Enhanced对象实例 * @param method intercepted Method * @param args argument array; primitive types are wrapped * @param proxy used to invoke super (non-intercepted method); may be called * as many times as needed * @throws Throwable any exception may be thrown; if so, super method will not be invoked * @return any value compatible with the signature of the proxied method. Method returning void will ignore this value. * @see MethodProxy */ public Object intercept(Object obj, java.lang.reflect.Method method, Object[] args,MethodProxy proxy) throws Throwable;
另外要补充的两点:
1. 方法拦截器对protected修饰的方法可以进行调用
2. 官方推荐在对委托方法进行调用时使用MethodProxy对方法进行调用。这样有两个好处:①官方说速度比较快 ②在intercept内调用委托类方法时不用保存委托对象引用
以上为本次对代理模式的总结,如有错误烦请指出纠正。转载请注明出处。
相关文章推荐
- ArcGIS Engine开发之旅04---ARCGIS接口详细说明
- C#中Monitor对象与Lock关键字的区别分析
- 【Redis】Redis的介绍与安装部署
- HttpClient
- mysqldump使用方法
- OpenCV之直方图均衡化原理详解
- 训练与测试
- 您的企业信息,安全吗?
- 巡检查看数据库Process 连接数
- 【MySQL】MySQL中针对大数据量常用技术
- UITableView 自定义分割线
- 使用外部拦截法处理滑动冲突
- jsp中中文乱码
- Python杨辉三角算法
- c# OpenFileDialog 打开多个文件
- POJ 2560 浮点型的带权值
- Cardboard使用空间音频(一)原文翻译
- AFN解析返回的html文件和ContentType是“text/plain”类型的json文件
- 前端开发人员必须了解的七大技术图谱
- lock,Monitor,Mutex的区别