Skip to content

Commit b04be58

Browse files
committed
代理
1 parent 0c44cec commit b04be58

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.duwei.designpattern.proxy.staticproxy;
2+
3+
/**
4+
* 静态代理
5+
*
6+
* 1.实现共同接口,或抽象类
7+
*
8+
* 2.代理类持有被代理类的抽象引用
9+
*/
10+
public class Main {
11+
12+
public static void main(String[] args) {
13+
14+
Proxy proxy = new Proxy();
15+
16+
proxy.proxyMethod();
17+
18+
}
19+
20+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.duwei.designpattern.proxy.staticproxy;
2+
3+
public class Proxy implements ProxyInterface {
4+
//这里持有真正对象的引用更清晰,注意和装饰器的区别
5+
private Target target;
6+
7+
public Proxy() {
8+
this.target = new Target();
9+
}
10+
11+
@Override
12+
public void proxyMethod() {
13+
target.proxyMethod();
14+
}
15+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.duwei.designpattern.proxy.staticproxy;
2+
3+
public interface ProxyInterface {
4+
5+
void proxyMethod();
6+
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.duwei.designpattern.proxy.staticproxy;
2+
3+
public class Target implements ProxyInterface {
4+
@Override
5+
public void proxyMethod() {
6+
System.out.println("我是被代理类");
7+
}
8+
}

0 commit comments

Comments
 (0)