Skip to content

Commit 97f4f8b

Browse files
limicalalimicala
authored andcommitted
代理
1 parent 9ba96ef commit 97f4f8b

6 files changed

Lines changed: 154 additions & 0 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.javacore.proxy;
2+
3+
import java.lang.reflect.InvocationHandler;
4+
import java.lang.reflect.Method;
5+
import java.lang.reflect.Proxy;
6+
7+
/**
8+
* 相关链接:(文章中的java版本可能比较老,但是只看关键代码也可以
9+
* https://www.ibm.com/developerworks/cn/java/j-lo-proxy1/
10+
*
11+
* http://www.cnblogs.com/flyoung2008/archive/2013/08/11/3251148.html
12+
*
13+
* https://www.zhihu.com/question/20794107
14+
*
15+
* jdk1.8
16+
* http://blog.csdn.net/bluetjs/article/details/52263410
17+
* 看这个http://rejoy.iteye.com/blog/1627405
18+
* 生成代理对象是用的Proxy类的静态方法newProxyInstance
19+
*
20+
*/
21+
public class DynamicProxy implements InvocationHandler {
22+
23+
private Object target;
24+
25+
public DynamicProxy(Object target){
26+
this.target = target;
27+
}
28+
29+
@Override
30+
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
31+
before();
32+
Object result = method.invoke(target, args);
33+
after();
34+
return result;
35+
}
36+
37+
/**
38+
* 生成代理对象
39+
* @return
40+
*/
41+
public Object getProxy(){
42+
return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
43+
target.getClass().getInterfaces(), this);
44+
}
45+
46+
public void before(){
47+
System.out.println("Before");
48+
}
49+
50+
public void after(){
51+
System.out.println("After");
52+
}
53+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.javacore.proxy;
2+
3+
public interface Hello {
4+
5+
String say(String name);
6+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.javacore.proxy;
2+
3+
public class HelloImpl implements Hello {
4+
@Override
5+
public String say(String name) {
6+
System.out.println("Hello" + name);
7+
return name;
8+
}
9+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.javacore.proxy;
2+
3+
/**
4+
* 静态代理
5+
*/
6+
public class HelloProxy implements Hello{
7+
private Hello hello;
8+
9+
public HelloProxy(){
10+
hello = new HelloImpl();
11+
}
12+
13+
public void before(){
14+
System.out.println("Before");
15+
}
16+
17+
public void after(){
18+
System.out.println("After");
19+
}
20+
21+
@Override
22+
public String say(String name) {
23+
before();
24+
hello.say("wu");
25+
after();
26+
return "";
27+
}
28+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# 代理模式
2+
3+
代理模式的作用是:
4+
5+
为其他对象提供一种代理以控制对这个对象的访问。
6+
在某些情况下,一个客户不想或者不能直接引用另一个对象,
7+
而代理对象可以在客户端和目标对象之间起到中介的作用。
8+
9+
代理分静态代理和动态代理
10+
使用场景可以看[链接](https://www.zhihu.com/question/20794107)
11+
12+
# 静态代理
13+
在调用真实角色的方法前或调用方法后执行一些操作,类似于AOP切面编程
14+
15+
因此,一个真实角色类对应一个代理角色类
16+
17+
18+
# 动态代理
19+
静态代理会导致代码量的增大,且许多代理类都是类似的
20+
21+
使用方法:
22+
- 编写一个动态代理类,实现InvocationHandler接口的invoke方法,
23+
24+
当代理类生成的代理对象执行真实角色类实现的接口的方法时,
25+
会调用invoke方法来实现
26+
27+
// 真实对象
28+
Hello hello = new HelloImpl();
29+
// 动态代理对象
30+
DynamicProxy proxy = new DynamicProxy(hello);
31+
// 使用Proxy的newProxyInstance方法来生成代理对象
32+
Hello helloproxy = (Hello) Proxy.newProxyInstance(
33+
hello.getClass().getClassLoader(),
34+
hello.getClass().getInterfaces(),
35+
proxy
36+
);
37+
// 调用Hello接口里面的方法
38+
helloproxy.say("aa");
39+
40+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.javacore.proxy;
2+
3+
import java.lang.reflect.Proxy;
4+
5+
public class DynamicProxyTest {
6+
7+
public static void main(String[] args){
8+
Hello hello = new HelloImpl();
9+
DynamicProxy proxy = new DynamicProxy(hello);
10+
11+
Hello helloproxy = (Hello) Proxy.newProxyInstance(
12+
hello.getClass().getClassLoader(),
13+
hello.getClass().getInterfaces(),
14+
proxy
15+
);
16+
helloproxy.say("aa");
17+
}
18+
}

0 commit comments

Comments
 (0)