Proxy Pattern

本文介绍了一个使用Java代理模式的具体示例。通过实现自定义的InvocationHandler接口并利用Proxy类创建代理对象,演示了如何对Collection接口的方法调用进行拦截,并记录方法名及参数。此外,还展示了一种更通用的代理工厂模式。
 
</span> 
</span> 
</span>
public class proxytest {		@suppresswarnings("unchecked")	public static void main(string[] args) {				collection collection = (collection)proxy.newproxyinstance(collection.class.getclassloader(), new class[]{collection.class}, new invocationhandler() {			arraylist list = new arraylist();			public object invoke(object proxy, method method, object[] args)					throws throwable {				system.out.println("方法:"+method.getname()+"   参数:"+arrays.tostring(args));				return method.invoke(list, args);			}		});		collection.add("aaa");		collection.add("aaa");		collection.add("aaa");		collection.add("aaa");		system.out.println(collection.size());	}}
 
<span>输出结果:</span>
<p class="msonormal" style="text-align: left; margin: 0cm 0cm 0pt 42pt;" align="left"><span>方法</span><span style="font-family: 'courier new'; color: #000000; font-size: 10pt;">:add<span>   </span></span><span>参数:</span><span style="font-family: 'courier new'; color: #000000; font-size: 10pt;">[aaa]</span>[/b]
public class myproxy {		public object getproxy(final object target,final advice advice){				object object = proxy.newproxyinstance(target.getclass().getclassloader(),target.getclass().getinterfaces(), new invocationhandler() {						@override			public object invoke(object proxy, method method, object[] args)					throws throwable {				advice.before();				object obj = method.invoke(target, args);				advice.after();				return obj;			}		});		return object;	}}
 
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值