一、概述
1.目标:要在Tank的move()方法做时间代理及日志代理(可以设想以后还要增加很多代理处理),且代理间的顺序可活更换
2.思路:
(1)聚合:代理类聚合了被代理类,且代理类及被代理类都实现了movable接口,则可实现灵活多变,具体看代码
(2)继承:继承不够灵活,随着功能需求增多,继承体系会非常臃肿。具体看代码
二、代码
1.Movable.java
2.Tank.java
3.TankTimeProxy.java
4.TankLogProxy.java
5.Tank2Time.java
6.Tank3Log.java
7.Client.java
1.Movable.java
1
2
3
|
public interface Movable { public void move(); } |
2.Tank.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
import java.util.Random; public class Tank implements Movable { @Override public void move() { System.out.println( "Tank moving......." ); try { Thread.sleep( new Random().nextInt( 5000 )); } catch (InterruptedException e) { e.printStackTrace(); } } } |
3.TankTimeProxy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
public class TankTimeProxy implements Movable { Movable m; public TankTimeProxy(Movable m) { this .m = m; } @Override public void move() { System.out.println( "Time Proxy start..........." ); long start = System.currentTimeMillis(); m.move(); long end = System.currentTimeMillis(); System.out.println( "花费时间:" +(end - start)); System.out.println( "Time Proxy end..........." ); } } |
4.TankLogProxy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public class TankLogProxy implements Movable { Movable m; public TankLogProxy(Movable m) { this .m = m; } @Override public void move() { System.out.println( "Log Proxy start..........." ); m.move(); System.out.println( "Log Proxy end..........." ); } } |
5.Tank2Time.java
1
2
3
4
5
6
7
8
9
10
11
|
public class Tank2Time extends Tank { public void move(){ System.out.println( "Tank2 time start..........." ); long start = System.currentTimeMillis(); super .move(); long end = System.currentTimeMillis(); System.out.println( "花费时间:" +(end - start)); System.out.println( "Tank2 time end..........." ); } } |
6.Tank3Log.java
1
2
3
4
5
6
7
8
|
public class Tank3Log extends Tank2Time { public void move(){ System.out.println( "Tank3Log start..........." ); super .move(); System.out.println( "Tank3Log end..........." ); } } |
7.Client.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
public class Client { @Test public void testProxy(){ Tank t = new Tank(); Movable m; //一、聚合的方式(较灵活,因为实现了接口) //1.1聚合方式的代理,先日志代理,后时间代理 TankTimeProxy ttp1 = new TankTimeProxy(t); TankLogProxy tlp1 = new TankLogProxy(ttp1); m = tlp1; m.move(); System.out.println( "\n==============================分隔线==========================\n" ); //1.2聚合方式的代理,先时间代理,后日志代理(可以灵活切换顺序) TankLogProxy tlp2 = new TankLogProxy(t); TankTimeProxy ttp2 = new TankTimeProxy(tlp2); m = ttp2; m.move(); System.out.println( "\n==============================分隔线==========================\n" ); //二、继承的方式 //2.1代理时间 Tank2Time t2 = new Tank2Time(); t2.move(); System.out.println( "\n==============================分隔线==========================\n" ); //2.2先代理日志,后时间,不能灵活切换 Tank3Log t3 = new Tank3Log(); t3.move(); } } |
三、运行结果
因为每个代理都实现了同一movable接口,代理和被代理对象之间都可以相互灵活转换,以实现代理功能之间的灵活叠加组合。
以上这篇java设计模式—静态代理模式(聚合与继承方式对比)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。