首页 > 代码库 > 【java设计模式】代理模式

【java设计模式】代理模式

计算类中方法运行时间的几种方案:

Client:

 1 package com.tn.proxy;
 2 
 3 public class Client {
 4     public static void main(String[] args) {
 5         /* Car car=new Car();
 6         car.move(); */
 7         
 8         //通过继承计算方法的运行时间
 9         /* CarTimeByExtends ctp=new CarTimeByExtends();
10         ctp.move(); */
11         
12         //通过聚合计算类中方法运行时间
13         /* Car car=new Car();
14         CarTimeByAggregate ctba=new CarTimeByAggregate(car);
15         ctba.getCarRunningTime(); */
16     }
17 }

Movable:

1 package com.tn.proxy;
2 
3 public interface Movable {
4     void move();
5     void stop();
6 }

Car:

 1 package com.tn.proxy;
 2 
 3 import java.util.Random;
 4 
 5 public class Car implements Movable {
 6 
 7     @Override
 8     public void move() {
 9         /**
10          * 方法内的两段注释代码为给move()方法增加日志及计算方法执行时间。
11          */
12         /* long start=System.currentTimeMillis();
13         System.out.println("Car is start moving..."+start); */
14         System.out.println("Car is moving...");
15         try {
16             Thread.sleep(new Random().nextInt(1500));
17         } catch (InterruptedException e) {
18             e.printStackTrace();
19         }
20         /* long end=System.currentTimeMillis();
21         System.out.println("Car is stop moving..."+end);
22         System.out.println("Car running time is "+(end-start)); */
23     }
24 
25     @Override
26     public void stop() {
27         System.out.println("Car is stopped.");
28     }
29 
30 }

CarTimeByExtends:

 1 package com.tn.proxy;
 2 /**
 3  * 通过继承计算类中方法的运行时间
 4  * @author xiongjiawei
 5  *
 6  */
 7 public class CarTimeByExtends extends Car{
 8     @Override
 9     public void move() {
10         long start=System.currentTimeMillis();
11         super.move();
12         long end=System.currentTimeMillis();
13         System.out.println("Car running time is:"+(end-start));
14     }
15 }

CarTimeByAggregate:

 1 package com.tn.proxy;
 2 /**
 3  * 通过聚合计算方法运行时间
 4  * @author xiongjiawei
 5  *
 6  */
 7 public class CarTimeByAggregate {
 8     Car car;
 9 
10     public CarTimeByAggregate(Car car){
11         this.car=car;
12     }
13     
14     public void getCarRunningTime(){
15         long start=System.currentTimeMillis();
16         car.move();
17         long end=System.currentTimeMillis();
18         System.out.println("Car running time is:"+(end-start));
19     }
20 }

 通过静态代理实现以上功能:

Client:

 1 package com.tn.proxy;
 2 
 3 public class Client {
 4     public static void main(String[] args) {
 5         Car car=new Car();
 6         CarTimeProxy ctp=new CarTimeProxy(car);
 7         CarLogProxy clp=new CarLogProxy(ctp);
 8         clp.move();
 9         /*    运行结果:Car被时间包装,时间被日志包装
10              logging...
11             start time:1494730233358
12             Car is moving...
13             end time:1494730234835
14             logged.
15          */
16         System.out.println("--------------------------------------");
17         Movable clp2=new CarLogProxy(car);
18         Movable ctp2=new CarTimeProxy(clp2);
19         ctp2.move();
20         /*
21         运行结果:时间包装日志,日志包装car
22         start time:1494730473747
23         logging...
24         Car is moving...
25         logged.
26         end time:1494730474995
27         */
28     }
29 }

Movable:

1 package com.tn.proxy;
2 
3 public interface Movable {
4     void move();
5 }

Car:

 1 package com.tn.proxy;
 2 
 3 import java.util.Random;
 4 
 5 public class Car implements Movable {
 6 
 7     @Override
 8     public void move() {
 9         System.out.println("Car is moving...");
10         try {
11             Thread.sleep(new Random().nextInt(1500));
12         } catch (InterruptedException e) {
13             e.printStackTrace();
14         }
15     }
16 }

CarTimeProxy:

 1 package com.tn.proxy;
 2 
 3 public class CarTimeProxy implements Movable{
 4     Movable movable;
 5     public CarTimeProxy(Movable movable){
 6         this.movable=movable;
 7     }
 8     @Override
 9     public void move() {
10         long start=System.currentTimeMillis();
11         System.out.println("start time:"+start);
12         movable.move();
13         long end=System.currentTimeMillis();
14         System.out.println("end time:"+end);
15     }
16 }

CarLogProxy:

 1 package com.tn.proxy;
 2 
 3 public class CarLogProxy implements Movable {
 4     Movable movable;
 5     
 6     public CarLogProxy(Movable movable){
 7         this.movable=movable;
 8     }
 9     
10     @Override
11     public void move() {
12         System.out.println("logging...");
13         movable.move();
14         System.out.println("logged.");
15     }
16 
17 }

 

【java设计模式】代理模式