Skip to content

Commit 18280dc

Browse files
author
laileon
committed
add
1 parent 4cf0ad3 commit 18280dc

File tree

9 files changed

+94
-0
lines changed

9 files changed

+94
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.blankj.custom.desingn_pattern.proxys.dynamicProxy;
2+
3+
import java.lang.reflect.InvocationHandler;
4+
5+
public class Client {
6+
public static void main(String[] args) {
7+
ISubject subject = new RealSubject();
8+
9+
InvocationHandler handler = new MyInvocationHandler(subject);
10+
ISubject proxy = DynamicProxy.newProxyInstance(subject.getClass().getClassLoader(), subject.getClass().getInterfaces(), handler);
11+
proxy.request();
12+
}
13+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.blankj.custom.desingn_pattern.proxys.dynamicProxy;
2+
3+
import java.lang.reflect.InvocationHandler;
4+
import java.lang.reflect.Proxy;
5+
6+
public class DynamicProxy<T> {
7+
@SuppressWarnings("unchecked")
8+
public static <T> T newProxyInstance(ClassLoader classLoader, Class<?>[] interfaces, InvocationHandler handler) {
9+
return (T) Proxy.newProxyInstance(classLoader, interfaces, handler);
10+
}
11+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.blankj.custom.desingn_pattern.proxys.dynamicProxy;
2+
3+
//https://www.jianshu.com/p/78fdd12c2c71
4+
public interface ISubject {
5+
void request();
6+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.blankj.custom.desingn_pattern.proxys.dynamicProxy;
2+
3+
import java.lang.reflect.InvocationHandler;
4+
import java.lang.reflect.Method;
5+
6+
public class MyInvocationHandler implements InvocationHandler {
7+
private Object target = null;
8+
9+
public MyInvocationHandler(Object object) {
10+
this.target = object;
11+
}
12+
13+
@Override
14+
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
15+
return method.invoke(this.target, args);
16+
}
17+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.blankj.custom.desingn_pattern.proxys.dynamicProxy;
2+
3+
public class RealSubject implements ISubject {
4+
@Override
5+
public void request() {
6+
System.out.println("我是真实对象");
7+
}
8+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.blankj.custom.desingn_pattern.proxys.staticProxy;
2+
3+
public class Client {
4+
public static void main(String[] args) {
5+
ISubject subject = new RealSubject();
6+
//让RealSubject这个借口的实现类来完成任务
7+
Proxy proxy = new Proxy(subject);
8+
proxy.request();
9+
}
10+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.blankj.custom.desingn_pattern.proxys.staticProxy;
2+
3+
//https://www.jianshu.com/p/78fdd12c2c71
4+
public interface ISubject {
5+
void request();
6+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.blankj.custom.desingn_pattern.proxys.staticProxy;
2+
3+
public class Proxy implements ISubject {
4+
private ISubject subject;
5+
6+
//传入接口的实现类
7+
public Proxy(ISubject subject) {
8+
this.subject = subject;
9+
}
10+
11+
@Override
12+
public void request() {
13+
this.subject.request();
14+
}
15+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.blankj.custom.desingn_pattern.proxys.staticProxy;
2+
3+
public class RealSubject implements ISubject{
4+
@Override
5+
public void request() {
6+
System.out.println("我是真实对象");
7+
}
8+
}

0 commit comments

Comments
 (0)