您的位置:首页 > 编程语言 > Java开发

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

2015-12-08 21:17 381 查看
这一章节我们讲述一下代理以及动态代理。
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.甚至我们可以通过方法名来控制执行的方法。

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

这一章节就到这里,谢谢。
-----------------------------------
目录
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java