项目结构:
运行效果:
=================================================
代码部分:
=================================================
/MovProxy/src/com/b510/movproxy/test/MovProxyTest.java
1 /** 2 * 3 */ 4 package com.b510.movproxy.test; 5 6 import com.b510.movproxy.dao.Moveable; 7 import com.b510.movproxy.dao.impl.Train; 8 import com.b510.movproxy.proxy.TrainLogProxy; 9 import com.b510.movproxy.proxy.TrainTimeProxy; 10 import com.b510.movproxy.proxy.TrainTranscationProxy; 11 12 /** 13 * 测试类 14 * 15 * @author hongten(hongtenzone@foxmail.com) 16 * @date 2013-2-24 17 */ 18 public class MovProxyTest { 19 public static void main(String[] args) throws Exception { 20 // (0.火车 1.事物 2.时间 3.日志) 从内向外格式 21 Train train = new Train(); 22 TrainTranscationProxy trainTranscationProxy = new TrainTranscationProxy(train); 23 TrainTimeProxy trainTimeProxy = new TrainTimeProxy(trainTranscationProxy); 24 TrainLogProxy trainLogProxy = new TrainLogProxy(trainTimeProxy); 25 Moveable moveable = trainLogProxy; 26 moveable.move(); 27 } 28 29 }
/MovProxy/src/com/b510/movproxy/dao/impl/Train.java
1 /** 2 * 3 */ 4 package com.b510.movproxy.dao.impl; 5 6 import java.util.Random; 7 8 import com.b510.movproxy.dao.Moveable; 9 10 /** 11 * 火车类 12 * @author hongten(hongtenzone@foxmail.com) 13 * @date 2013-2-24 14 */ 15 public class Train implements Moveable{ 16 17 public void move() throws Exception{ 18 System.out.println("the Train begin to move ......"); 19 long timer = new Random().nextInt(15000); 20 try { 21 Thread.sleep(timer); 22 } catch (InterruptedException e) { 23 e.printStackTrace(); 24 } 25 System.out.println("the Train end to move ......" + timer); 26 } 27 28 }
/MovProxy/src/com/b510/movproxy/dao/Moveable.java
1 /** 2 * 3 */ 4 package com.b510.movproxy.dao; 5 6 /** 7 * @author hongten(hongtenzone@foxmail.com) 8 * @date 2013-2-24 9 */ 10 public interface Moveable { 11 12 public void move() throws Exception; 13 }
/MovProxy/src/com/b510/movproxy/proxy/TrainTranscationProxy.java
1 /** 2 * 3 */ 4 package com.b510.movproxy.proxy; 5 6 import java.util.Random; 7 8 import com.b510.movproxy.dao.Moveable; 9 10 /** 11 * 火车的事物代理 12 * @author hongten(hongtenzone@foxmail.com) 13 * @date 2013-2-24 14 */ 15 public class TrainTranscationProxy implements Moveable{ 16 17 /** 18 * Moveable接口 19 */ 20 private Moveable moveable; 21 22 /** 23 * 在构造方法中传递Moveable 24 */ 25 public TrainTranscationProxy(Moveable moveable){ 26 this.moveable = moveable; 27 } 28 29 public void move() throws Exception { 30 System.out.println("the transcation begin......"); 31 long timer = new Random().nextInt(15000); 32 try { 33 moveable.move(); 34 Thread.sleep(timer); 35 } catch (InterruptedException e) { 36 e.printStackTrace(); 37 } 38 System.out.println("the transcation end ....." + timer); 39 40 } 41 42 43 }
/MovProxy/src/com/b510/movproxy/proxy/TrainTimeProxy.java
1 /** 2 * 3 */ 4 package com.b510.movproxy.proxy; 5 6 import com.b510.movproxy.dao.Moveable; 7 8 /** 9 * 火车的时间代理 10 * @author hongten(hongtenzone@foxmail.com) 11 * @date 2013-2-24 12 */ 13 public class TrainTimeProxy implements Moveable{ 14 /** 15 * Moveable接口 16 */ 17 private Moveable moveable; 18 19 /** 20 * 在构造方法中传递Moveable 21 */ 22 public TrainTimeProxy(Moveable moveable){ 23 this.moveable = moveable; 24 } 25 26 public void move() throws Exception { 27 long start = System.currentTimeMillis(); 28 System.out.println("the time of start ........."); 29 moveable.move(); 30 long end = System.currentTimeMillis(); 31 System.out.println("the result time :" + (end - start)); 32 33 } 34 35 36 37 }
/MovProxy/src/com/b510/movproxy/proxy/TrainLogProxy.java
1 /** 2 * 3 */ 4 package com.b510.movproxy.proxy; 5 6 import com.b510.movproxy.dao.Moveable; 7 8 /** 9 * 火车的日志代理 10 * @author hongten(hongtenzone@foxmail.com) 11 * @date 2013-2-24 12 */ 13 public class TrainLogProxy implements Moveable{ 14 15 /** 16 * Moveable接口 17 */ 18 private Moveable moveable; 19 20 /** 21 * 在构造方法中传递Moveable 22 */ 23 public TrainLogProxy(Moveable moveable){ 24 this.moveable = moveable; 25 } 26 27 public void move() throws Exception { 28 System.out.println("start to log of the move() method!!!"); 29 moveable.move(); 30 System.out.println("stop to log of the move() method!!!"); 31 } 32 33 34 }