【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();
}
}
【java设计模式】代理模式的更多相关文章
- Java设计模式-代理模式之动态代理(附源代码分析)
Java设计模式-代理模式之动态代理(附源代码分析) 动态代理概念及类图 上一篇中介绍了静态代理,动态代理跟静态代理一个最大的差别就是:动态代理是在执行时刻动态的创建出代理类及其对象. 上篇中的静态代 ...
- JAVA 设计模式 代理模式
用途 代理模式 (Proxy) 为其他对象提供一种代理以控制对这个对象的访问. 代理模式是一种结构型模式. 结构
- Java设计模式の代理模式
目录 代理模式 1.1.静态代理 1.2.动态代理 1.3.Cglib代理 代理模式 代理(Proxy)是一种设计模式,提供了对目标对象另外的访问方式;即通过代理对象访问目标对象.这样做的好处是 ...
- Java设计模式 - 代理模式
1.什么是代理模式: 为另一个对象提供一个替身或占位符以访问这个对象. 2.代理模式有什么好处: (1)延迟加载 当你需要从网络上面查看一张很大的图片时,你可以使用代理模式先查看它的缩略图看是否是自己 ...
- Java设计模式—代理模式
代理模式(Proxy Pattern)也叫做委托模式,是一个使用率非常高的模式. 定义如下: 为其他对象提供一种代理以控制对这个对象的访问. 个人理解: 代理模式将原类进行封装, ...
- Java设计模式——代理模式实现及原理
简介 Java编程的目标是实现现实不能完成的,优化现实能够完成的,是一种虚拟技术.生活中的方方面面都可以虚拟到代码中.代理模式所讲的就是现实生活中的这么一个概念:中介. 代理模式的定义:给某一个对象提 ...
- Java设计模式-代理模式(Proxy)
其实每个模式名称就表明了该模式的作用,代理模式就是多一个代理类出来,替原对象进行一些操作,比如我们在租房子的时候回去找中介,为什么呢?因为你对该地区房屋的信息掌握的不够全面,希望找一个更熟悉的人去帮你 ...
- Java设计模式--代理模式+动态代理+CGLib代理
静态代理 抽象主题角色:声明真实主题和代理主题的共同接口. 代理主题角色:代理主题内部含有对真实主题的引用,从而在任何时候操作真实主题对象:代理主题提供一个与真实主题相同的接口,以便在任何时候都可以代 ...
- Java设计模式——代理模式
public interface People { public void work(); } public class RealPeople implements People { public v ...
- Java 之 设计模式——代理模式
设计模式——代理模式 一.概述 1.代理模式 (1)真实对象:被代理的对象 (2)代理对象:代理真实对象的 (3)代理模式:代理对象代理真实对象,达到增强真实对象功能的目的 二.实现方式 1.静态代理 ...
随机推荐
- 深入理解Postgres中的cache
众所周知,缓存是提高数据库性能的一个重要手段.本文着重讲一讲PostgreSQL中的缓存相关的东西.当然万变不离其宗,原理都是共同的,理解了这些,你也很容易把它运用到其它数据库中. What is a ...
- Natural Merge Sort(自然归并排序)
This is a Natural Merge Sort program from my textbook. It works, but I don't think it's good. // Nat ...
- 书写规范的javaScript
书写可维护的代码 代码的维护,修改以及扩展都是需要时间和人力成本的,所以为了减少浪费不必要的成本,从一开始就要书写可维护的代码,这样给自己也给项目其他人提供便利. 书写可维护的代码意味着你的代码是: ...
- centos6.7安装openblas错误
centos系统:CentOS release 6.7 (Final)安装OpenBLAS # Install OpenBLAS at /usr/local/openblas git clone ht ...
- SQL知识目录
SQL理论知识 -------理论知识总结 -------理论知识总结 -------理论知识总结 -------理论知识总结 -------理论知识总结 -------理论知识总结 -------理 ...
- MySQL的外键,修改表,基本数据类型,表级别操作,其他(条件,通配符,分页,排序,分组,联合,连表操作)
MySQL的外键,修改表,基本数据类型,表级别操作,其他(条件,通配符,分页,排序,分组,联合,连表操作): a.创建2张表 create table userinfo(nid int not nul ...
- javaScript基础概念小知识点集
数据类型 typeof是一个操作符而不是函数,因此例子中圆括号尽管可以使用,但不是必须的. 只要在保存对象的变量还没有真正保存对象,就应该明确的让该变量保存null NaN是一个特殊的数值,与任何值都 ...
- strcmp函数
strcmp函数用于c语言中两个字符串比较(只可以比较字符串,不可以比较数字) 规则 当s1>s2时,返回为正数: 当s1=s2时,返回值为0: 当s1<s2时,返回为负数: 两个字符串自 ...
- SEO是件贼有意思的事情 golang入坑系列
这两天迷上了SEO.真心看不起百度的竞价排名,但作为一个商业网站,赚钱是一件无可厚非的事情.只做活雷锋,没有大金主是做不长的.做完功课后,发现百度和google的SEO策略又不相同,几乎是无法通用.百 ...
- 微信JS-SDK选择相册或拍照并上传PHP实现
理解:微信上传接口是拍照,或者选择本地照片,上传到微信的服务器,获取到一个id,通过token与这个id获取到图片,保存到服务器即可. 效果 通过微信js接口,调用底层程序. 需要引入js文件,并进行 ...