代理模式之InvocationHandler
来源:互联网 发布:手机淘宝 国际版 编辑:程序博客网 时间:2024/06/05 06:39
package com.gewb.proxy;public interface Moveable {void move();}
package com.gewb.proxy;import java.util.Random;public class Car implements Moveable{@Overridepublic void move() {try {System.out.println("汽车行驶中。。。");Thread.sleep(new Random().nextInt(1000));} catch (InterruptedException e) {e.printStackTrace();}}}
package com.gewb.proxy;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;public class TimeHandler implements InvocationHandler {private Object target;public TimeHandler(Object target) {super();this.target = target;}/** * proxy 被代理对象 * method 被代理对象方法 * args 被代理对象方法参数 */@Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {long starttime = System.currentTimeMillis();System.out.println("汽车开始行驶");method.invoke(target);long endtime = System.currentTimeMillis();System.out.println("汽车行驶结束");System.out.println("用时" + (endtime - starttime) + "毫秒");return null;}}
package com.gewb.proxy;public class LogHandler implements Moveable {private Moveable m;public LogHandler(Moveable m) {super();this.m = m;}@Overridepublic void move() {System.out.println("日志记录开始");m.move();System.out.println("日志记录结束");}}
package com.gewb.proxy;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Proxy;public class Main {public static void main(String[] args) {Car car = new Car();LogHandler h = new LogHandler(car);InvocationHandler handler = new TimeHandler(h);Moveable instance = (Moveable) Proxy.newProxyInstance(car.getClass().getClassLoader(), car.getClass().getInterfaces(), handler);instance.move();
1 0
- 代理模式之InvocationHandler
- InvocationHandler,proxy之动态代理
- InvocationHandler+工厂设计模式 实现动态代理
- JAVA动态代理之:接口InvocationHandler
- 动态代理 Proxy InvocationHandler
- 动态代理proxy ,InvocationHandler
- 动态代理 Proxy InvocationHandler
- 关于InvocationHandler动态代理
- 动态代理模式(抽象角色用接口形式,代理角色必须实现InvocationHandler)
- 代理模式及JDK动态代理(InvocationHandler)的简单实现与分析
- java动态代理-InvocationHandler Proxy
- Java动态代理InvocationHandler+Proxy
- 马士兵动态代理InvocationHandler
- 动态代理 Proxy InvocationHandler学习
- 动态代理 Proxy, InvocationHandler 用法
- JDK动态代理proxy,InvocationHandler
- 动态代理 Proxy InvocationHandler学习
- java代理 InvocationHandler接口实现
- 如何锁定横屏或竖屏
- Python 3基础教程34-tkinter添加一个按钮
- 模板方法模式
- Coherence EP中调用其他CACHE问题deadlock告警问题
- HashMap实现原理分析
- 代理模式之InvocationHandler
- 自定义View圆形进度条
- Java 线程
- 学习目标的层次分类
- Lucene (一)建立索引及应用的属性详解
- 折半查找小程序
- 290. Word Pattern
- leetcode 62-所有可行的路径数
- BNU 51645 ACM Battle【思维+爆搜】确实挺隐秘的一个爆搜题