【java设计模式】代理模式

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

Client:

 package com.tn.proxy;

 public class Client {
public static void main(String[] args) {
/* Car car=new Car();
car.move(); */ //通过继承计算方法的运行时间
/* CarTimeByExtends ctp=new CarTimeByExtends();
ctp.move(); */ //通过聚合计算类中方法运行时间
/* Car car=new Car();
CarTimeByAggregate ctba=new CarTimeByAggregate(car);
ctba.getCarRunningTime(); */
}
}

Movable:

 package com.tn.proxy;

 public interface Movable {
void move();
void stop();
}

Car:

 package com.tn.proxy;

 import java.util.Random;

 public class Car implements Movable {

     @Override
public void move() {
/**
* 方法内的两段注释代码为给move()方法增加日志及计算方法执行时间。
*/
/* long start=System.currentTimeMillis();
System.out.println("Car is start moving..."+start); */
System.out.println("Car is moving...");
try {
Thread.sleep(new Random().nextInt(1500));
} catch (InterruptedException e) {
e.printStackTrace();
}
/* long end=System.currentTimeMillis();
System.out.println("Car is stop moving..."+end);
System.out.println("Car running time is "+(end-start)); */
} @Override
public void stop() {
System.out.println("Car is stopped.");
} }

CarTimeByExtends:

 package com.tn.proxy;
/**
* 通过继承计算类中方法的运行时间
* @author xiongjiawei
*
*/
public class CarTimeByExtends extends Car{
@Override
public void move() {
long start=System.currentTimeMillis();
super.move();
long end=System.currentTimeMillis();
System.out.println("Car running time is:"+(end-start));
}
}

CarTimeByAggregate:

 package com.tn.proxy;
/**
* 通过聚合计算方法运行时间
* @author xiongjiawei
*
*/
public class CarTimeByAggregate {
Car car; public CarTimeByAggregate(Car car){
this.car=car;
} public void getCarRunningTime(){
long start=System.currentTimeMillis();
car.move();
long end=System.currentTimeMillis();
System.out.println("Car running time is:"+(end-start));
}
}

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

Client:

 package com.tn.proxy;

 public class Client {
public static void main(String[] args) {
Car car=new Car();
CarTimeProxy ctp=new CarTimeProxy(car);
CarLogProxy clp=new CarLogProxy(ctp);
clp.move();
/* 运行结果:Car被时间包装,时间被日志包装
logging...
start time:1494730233358
Car is moving...
end time:1494730234835
logged.
*/
System.out.println("--------------------------------------");
Movable clp2=new CarLogProxy(car);
Movable ctp2=new CarTimeProxy(clp2);
ctp2.move();
/*
运行结果:时间包装日志,日志包装car
start time:1494730473747
logging...
Car is moving...
logged.
end time:1494730474995
*/
}
}

Movable:

 package com.tn.proxy;

 public interface Movable {
void move();
}

Car:

 package com.tn.proxy;

 import java.util.Random;

 public class Car implements Movable {

     @Override
public void move() {
System.out.println("Car is moving...");
try {
Thread.sleep(new Random().nextInt(1500));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

CarTimeProxy:

 package com.tn.proxy;

 public class CarTimeProxy implements Movable{
Movable movable;
public CarTimeProxy(Movable movable){
this.movable=movable;
}
@Override
public void move() {
long start=System.currentTimeMillis();
System.out.println("start time:"+start);
movable.move();
long end=System.currentTimeMillis();
System.out.println("end time:"+end);
}
}

CarLogProxy:

 package com.tn.proxy;

 public class CarLogProxy implements Movable {
Movable movable; public CarLogProxy(Movable movable){
this.movable=movable;
} @Override
public void move() {
System.out.println("logging...");
movable.move();
System.out.println("logged.");
} }

利用反射动态加载:

 package com.tn.proxy2;

 import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.net.URLClassLoader; import javax.tools.JavaCompiler;
import javax.tools.JavaCompiler.CompilationTask; import com.tn.proxy.Car;
import com.tn.proxy.Movable; import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider; public class Test {
public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
String rt="\r\t";
String str=
"package com.tn.proxy;"+rt+ "public class CarTimeProxy implements Movable{"+rt+
" Movable movable;"+rt+
" public CarTimeProxy(Movable movable){"+rt+
" this.movable=movable;"+rt+
" }"+rt+
" @Override"+rt+
" public void move() {"+rt+
" long start=System.currentTimeMillis();"+rt+
" System.out.println(\"start time:\"+start);"+rt+
" movable.move();"+rt+
" long end=System.currentTimeMillis();"+rt+
" System.out.println(\"end time:\"+end);"+rt+
" }"+rt+
"}"; //源代码文件生成
String fileName=System.getProperty("user.dir")
+"/src/com/tn/proxy/CarTimeProxy.java";
// System.out.println(fileName);
File f=new File(fileName);
FileWriter fw=new FileWriter(f);
fw.write(str);
fw.flush();
fw.close(); //编译
JavaCompiler compiler=ToolProvider.getSystemJavaCompiler();
// System.out.println(compiler.getClass().getName());
StandardJavaFileManager fMgr=compiler.getStandardFileManager(null, null, null);
Iterable iterable=fMgr.getJavaFileObjects(fileName);
CompilationTask ct=compiler.getTask(null, fMgr, null, null, null, iterable);
ct.call();
fMgr.close(); //载入内存,创建类对象实例
URL[] urls=new URL[]{new URL("file:/"+System.getProperty("user.dir")+"/src")};
URLClassLoader ul=new URLClassLoader(urls);
Class c=ul.loadClass("com.tn.proxy.CarTimeProxy");
ul.close();
// System.out.println(c); Constructor constructor=c.getConstructor(Movable.class);
Movable movable=(Movable)constructor.newInstance(new Car());
movable.move();
}
}
上一篇:的理解和使用


下一篇:Unity3d 换装Avatar系统