Skip to content

Commit 26d56e9

Browse files
author
laileon
committed
设计模式: Proxy
1 parent 57b6b95 commit 26d56e9

File tree

10 files changed

+93
-1
lines changed

10 files changed

+93
-1
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.blankj.csutom.Proxy.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.csutom.Proxy.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.csutom.Proxy.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.csutom.Proxy.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.csutom.Proxy.dynamicProxy;
2+
3+
public class RealSubject implements ISubject {
4+
@Override
5+
public void request() {
6+
System.out.println("我是真实对象");
7+
}
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.blankj.csutom.Proxy.staticProxy;
2+
3+
public class Client {
4+
public static void main(String[] args) {
5+
ISubject subject = new RealSubject();
6+
Proxy proxy = new Proxy(subject);
7+
proxy.request();
8+
}
9+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.blankj.csutom.Proxy.staticProxy;
2+
3+
//https://www.jianshu.com/p/78fdd12c2c71
4+
public interface ISubject {
5+
void request();
6+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.blankj.csutom.Proxy.staticProxy;
2+
3+
public class Proxy implements ISubject {
4+
private ISubject subject;
5+
6+
public Proxy(ISubject subject) {
7+
this.subject = subject;
8+
}
9+
10+
@Override
11+
public void request() {
12+
this.subject.request();
13+
}
14+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.blankj.csutom.Proxy.staticProxy;
2+
3+
public class RealSubject implements ISubject{
4+
@Override
5+
public void request() {
6+
System.out.println("我是真实对象");
7+
}
8+
}

src/com/blankj/medium/_017/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* </pre>
1414
*/
1515
public class Solution {
16-
// private static String[] map = new String[]{"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
16+
// private staticProxy String[] map = new String[]{"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
1717
//
1818
// public List<String> letterCombinations(String digits) {
1919
// if (digits.length() == 0) return Collections.emptyList();

0 commit comments

Comments
 (0)