从头认识java-12.5 代理以及动态代理

这一章节我们讲述一下代理以及动态代理。

1.为什么需要代理?

答案:有一些操作你不想整合到逻辑代码里面,因此需要代理来做这个事情。例如:日志、监控等

下面将演示一个简单的代理:

package com.ray.ch11;

public class Test {
	public static void test(InterFace interFace) {
		interFace.doSomething();
		interFace.doElsething();
	}

	public static void main(String[] args) {
		test(new RealObject());
		test(new SimpleProxy(new RealObject()));
	}
}

interface InterFace {
	void doSomething();

	void doElsething();
}

class RealObject implements InterFace {

	@Override
	public void doSomething() {
		System.out.println("RealObject doSomething");
	}

	@Override
	public void doElsething() {
		System.out.println("RealObject doElsething");
	}
}

class SimpleProxy implements InterFace {
	private InterFace interFace;

	public SimpleProxy(InterFace interFace) {
		this.interFace = interFace;
	}

	@Override
	public void doSomething() {
		System.out.println("SimpleProxy doSomething");
		interFace.doSomething();
	}

	@Override
	public void doElsething() {
		System.out.println("SimpleProxy doElsething");
		interFace.doElsething();
	}
}

输出:

RealObject doSomething
RealObject doElsething
SimpleProxy doSomething
RealObject doSomething
SimpleProxy doElsething
RealObject doElsething

从上面的代码可以看见,只需要真实对象与代理同时实现一个接口,就满足实现代理的基本要求,其实我们上面的代码在代理里面的两句输出,都是我们不想整合到逻辑代码里面的,因此需要代理这个类来处理这些事情。


2.动态代理

java里面不单是存在代理这个模式,而且还可以动态的创建代理。

我们看下面的代码,我们使用java的动态代理替代了我们上面的静态代理。

package com.ray.ch11;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class Test {
	public static void test(InterFace interFace) {
		interFace.doSomething();
		interFace.doElsething();
	}

	public static void main(String[] args) {
		InterFace proxy = (InterFace) Proxy.newProxyInstance(InterFace.class
				.getClassLoader(), new Class[] { InterFace.class },
				new DynamicProxyHandler(new RealObject()));
		test(proxy);
	}
}

class DynamicProxyHandler implements InvocationHandler {
	private InterFace interFace;

	public DynamicProxyHandler(InterFace interFace) {
		this.interFace = interFace;
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable {
		System.out.println("proxy dosomething");
		System.out.println(proxy.getClass());
		System.out.println(method);
		return method.invoke(interFace, args);
	}
}

interface InterFace {
	void doSomething();

	void doElsething();
}

class RealObject implements InterFace {

	@Override
	public void doSomething() {
		System.out.println("RealObject doSomething");
	}

	@Override
	public void doElsething() {
		System.out.println("RealObject doElsething");
	}
}

输出:

dosomething
class com.ray.ch11.$Proxy0
public abstract void com.ray.ch11.InterFace.doSomething()
RealObject doSomething
dosomething
class com.ray.ch11.$Proxy0
public abstract void com.ray.ch11.InterFace.doElsething()
RealObject doElsething


上面的代码一样实现了第一点所说的功能,但是有一些需要注意的,就是Proxy.newProxyInstance里面的几个参数

1.是classLoader,这个loader一般是已经加载对象的loader,上面的loader除了可以是InterFace的loader,也可以是Test的loader

2.你希望实现的接口列表(这里一定是接口,而不是类或者抽象类)

3.InvocationHandler的实现

4.在DynamicProxyHandler的实现里面proxy其实是代理,而不是实际的类

5.通过method.invoke(interFace, args)方法执行真实类的方法

6.甚至我们可以通过方法名来控制执行的方法。


总结:我们这一章节讲述了代理以及动态代理。


这一章节就到这里,谢谢。

-----------------------------------

目录



转载于:https://my.oschina.net/u/2325575/blog/543729

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值