• 【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 }

    利用反射动态加载:

     1 package com.tn.proxy2;
     2 
     3 import java.io.File;
     4 import java.io.FileWriter;
     5 import java.io.IOException;
     6 import java.lang.reflect.Constructor;
     7 import java.lang.reflect.InvocationTargetException;
     8 import java.net.URL;
     9 import java.net.URLClassLoader;
    10 
    11 import javax.tools.JavaCompiler;
    12 import javax.tools.JavaCompiler.CompilationTask;
    13 
    14 import com.tn.proxy.Car;
    15 import com.tn.proxy.Movable;
    16 
    17 import javax.tools.StandardJavaFileManager;
    18 import javax.tools.ToolProvider;
    19 
    20 public class Test {
    21     public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    22         String rt="
    	";
    23         String str=
    24                 "package com.tn.proxy;"+rt+
    25 
    26         "public class CarTimeProxy implements Movable{"+rt+
    27         "    Movable movable;"+rt+
    28         "    public CarTimeProxy(Movable movable){"+rt+
    29         "        this.movable=movable;"+rt+
    30         "    }"+rt+
    31         "    @Override"+rt+
    32         "    public void move() {"+rt+
    33         "        long start=System.currentTimeMillis();"+rt+
    34         "        System.out.println("start time:"+start);"+rt+
    35         "        movable.move();"+rt+
    36         "        long end=System.currentTimeMillis();"+rt+
    37         "        System.out.println("end time:"+end);"+rt+
    38         "    }"+rt+
    39         "}";
    40         
    41         //源代码文件生成
    42         String fileName=System.getProperty("user.dir")
    43                         +"/src/com/tn/proxy/CarTimeProxy.java";
    44 //        System.out.println(fileName);
    45         File f=new File(fileName);
    46         FileWriter fw=new FileWriter(f);
    47         fw.write(str);
    48         fw.flush();
    49         fw.close();
    50         
    51         //编译
    52         JavaCompiler compiler=ToolProvider.getSystemJavaCompiler();
    53 //        System.out.println(compiler.getClass().getName());
    54         StandardJavaFileManager fMgr=compiler.getStandardFileManager(null, null, null);
    55         Iterable iterable=fMgr.getJavaFileObjects(fileName);
    56         CompilationTask ct=compiler.getTask(null, fMgr, null, null, null, iterable);
    57         ct.call();
    58         fMgr.close();
    59         
    60         //载入内存,创建类对象实例
    61         URL[] urls=new URL[]{new URL("file:/"+System.getProperty("user.dir")+"/src")};
    62         URLClassLoader ul=new URLClassLoader(urls);
    63         Class c=ul.loadClass("com.tn.proxy.CarTimeProxy");
    64         ul.close();
    65 //        System.out.println(c);
    66         
    67         Constructor constructor=c.getConstructor(Movable.class);
    68         Movable movable=(Movable)constructor.newInstance(new Car());
    69         movable.move();
    70     }
    71 }
  • 相关阅读:
    三道趣味题目
    iOS开发中使用静态库 .a 文件
    java Graphics2D 画图
    堆和栈的区别
    iOS开发中KVC、KVO简介
    GPUImage的简单使用
    OC中 self.view.frame.size.height = 100; 不能通过编译的原因
    Xcode7 低版本iOS系统上下有黑边的问题
    c语言数组赋值
    ELF interpreter /libexec/ld-elf32.so.1 not found
  • 原文地址:https://www.cnblogs.com/xiongjiawei/p/6851802.html
Copyright © 2020-2023  润新知