jdk动态代理实现作业怎么写
作业怎么实现有同时记录时间和日子?
作业怎么实现有同时记录时间和日子?
 
                            2016-07-24
package com.jdkproxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
/*
 * 日志
 */
public class LogHandler implements InvocationHandler {
	
	private Object target;
	public LogHandler (Object target){
		super();
		this.target = target;
	}
	public Object getTarget() {
		return target;
	}
	public void setTarget(Object target) {
		this.target = target;
	}
	@Override
	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable {
		System.out.println("日志记录开始。。。");
		method.invoke(target);
		System.out.println("日志记录结束。。。");
		return null;
	}
}package com.jdkproxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import com.proxy.Car;
import com.proxy.Moveable;
public class Text {
	/**
	 * JDK动态代理测试类
	 * 
	 * 动态代理实现步骤
	 * 1.创建一个实现InvocationHandler接口的类,它必须实现invoke方法
	 * 2.创建被代理的类和接口
	 * 3.调用Proxy的静态方法,创建一个代理类newProxyInstance()
	 * 4.通过代理调用方法
	 * 
	 */
	public static void main(String[] args) {
		Car car = new Car();
		InvocationHandler h = new TimeHandler(car);
		Class<?> cls= car.getClass();
		/*
		 * loader 类加载器
		 * interfaces 实现接口
		 * h InvocationHandler
		 */
		Moveable m = (Moveable)Proxy.newProxyInstance(cls.getClassLoader(), cls.getInterfaces(), h);
		
		//同时实现时间和日志的动态代理
		InvocationHandler l = new LogHandler(m);
		Moveable m1 = (Moveable)Proxy.newProxyInstance(cls.getClassLoader(), cls.getInterfaces(), l);
		m1.move();
	}
}package com.jdkproxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
/*
* 日志
*/
public class LogHandler implements InvocationHandler {
private Object target;
public LogHandler (Object target){
super();
this.target = target;
}
public Object getTarget() {
return target;
}
public void setTarget(Object target) {
this.target = target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
System.out.println("日志记录开始。。。");
method.invoke(target);
System.out.println("日志记录结束。。。");
return null;
}
}
public class Test {
	public static void main(String[] args) {
		Car car = new Car();
		//timeHandler是添加了time的car代理
		InvocationHandler timeHandler=  new TimeProxy(car);
		Class<?> cls = car.getClass();
		
		Move timemove = (Move) Proxy.newProxyInstance(cls.getClassLoader(), cls.getInterfaces(), timeHandler);
		//注意此处timemove代替car,logHandler则为添加了time和log的car代理
		InvocationHandler logHandler =  new LogProxy(timemove);
		
		/**
		 * loader:类加载器,
		 * interfaces:实现接口
		 * h:InvocationHandler
		 */
		
		Move logmove = (Move) Proxy.newProxyInstance(cls.getClassLoader(), cls.getInterfaces(), logHandler);
		logmove.move();
	}
}public static void main(String[] args) {
		Car car = new Car();
		InvocationHandler h =  new JDKProxy(car);
		Class<?> cls = car.getClass();
		Move m = (Move) Proxy.newProxyInstance(cls.getClassLoader(), cls.getInterfaces(), h);
		InvocationHandler h1 =  new LogProxy(m);
		
		/**
		 * loader:类加载器,
		 * interfaces:实现接口
		 * h:InvocationHandler
		 */
		
		Move m1 = (Move) Proxy.newProxyInstance(cls.getClassLoader(), cls.getInterfaces(), h1);
		m1.move();
	}举报