Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b1e7ee6

Browse files
author
deeper
committed
dynamic_proxy create complete
1 parent 96fa0e3 commit b1e7ee6

File tree

4 files changed

+75
-1
lines changed

4 files changed

+75
-1
lines changed
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
package com.ruoxu.pattern.proxy.dynamic_proxy;
22

3+
import java.lang.reflect.Proxy;
4+
35
/**
46
* 动态代理
5-
*
67
*
78
*/
89
public class Demo {
910
public static void main(String[] args) {
11+
ILawsuit poorMan = new PoorMan();
12+
// 构造一个动态代理
13+
DynamicProxy dynamicProxy = new DynamicProxy(poorMan);
14+
// 获得被代理类的ClassLoader
15+
ClassLoader classLoader = poorMan.getClass().getClassLoader();
16+
// 动态构造一个动态律师代理类
17+
ILawsuit lawyerProxy = (ILawsuit) Proxy.newProxyInstance(classLoader, new Class[]{ILawsuit.class}, dynamicProxy);
1018

19+
lawyerProxy.submit();
20+
lawyerProxy.burden();
21+
lawyerProxy.defend();
22+
lawyerProxy.finish();
1123
}
24+
1225
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.ruoxu.pattern.proxy.dynamic_proxy;
2+
3+
import java.lang.reflect.InvocationHandler;
4+
import java.lang.reflect.Method;
5+
6+
public class DynamicProxy implements InvocationHandler{
7+
private Object mObject; // 被代理类的引用
8+
public DynamicProxy(Object object) {
9+
this.mObject = object;
10+
}
11+
@Override
12+
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
13+
// 调用被代理类的方法
14+
System.out.print("【===Proxy ");
15+
Object object = method.invoke(mObject, args);
16+
System.out.println("===】");
17+
return object;
18+
}
19+
20+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.ruoxu.pattern.proxy.dynamic_proxy;
2+
/**
3+
* 诉讼接口类
4+
*
5+
*/
6+
public interface ILawsuit {
7+
// 提交申请
8+
void submit();
9+
// 进行举证
10+
void burden();
11+
// 开始辩护
12+
void defend();
13+
// 诉讼完成
14+
void finish();
15+
16+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.ruoxu.pattern.proxy.dynamic_proxy;
2+
3+
public class PoorMan implements ILawsuit{
4+
private static final String PREFIX = "poorMan ";
5+
6+
@Override
7+
public void submit() {
8+
System.out.print(PREFIX+"submit");
9+
}
10+
11+
@Override
12+
public void burden() {
13+
System.out.print(PREFIX+"burden");
14+
}
15+
16+
@Override
17+
public void defend() {
18+
System.out.print(PREFIX+"defend");
19+
}
20+
21+
@Override
22+
public void finish() {
23+
System.out.print(PREFIX+"finish");
24+
}
25+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /