File tree Expand file tree Collapse file tree 4 files changed +50
-0
lines changed
src/com/duwei/designpattern/proxy/staticproxy Expand file tree Collapse file tree 4 files changed +50
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ package com .duwei .designpattern .proxy .staticproxy ;
2+
3+ public interface ProxyInterface {
4+
5+ void proxyMethod ();
6+
7+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments