动态代理之代理工厂实现

Waiter.java

package cn.itcast.demo3;

//服务员
public interface Waiter {
	//服务
	public void serve();

}

ManWaiter.java

package cn.itcast.demo3;

public class ManWaiter implements Waiter {

	public void serve() {
		System.out.println("服务中...");
	}
}

前置增强接口BeforeAdvice.java(这样的话,增强的内容就可以动态的改变了)

package cn.itcast.demo3;

public interface BeforeAdvice {
	public void before();
}

后置增强接口AfterAdvice.java

package cn.itcast.demo3;

public interface AfterAdvice {
	public void after();
}

今天主菜 ProxyFactory.java

package cn.itcast.demo3;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 代理工厂
 * 它用来生成代理对象
 * 他需要所有的参数
 * @author WenHuagang
 * 
 * 
 * 1.创建代理工厂
 * 2.给工厂设置三样东西:
 * 	*目标对象:setTargetObject()
 * 	*前置增强:setBeforeAdvice(BeforeAdvice接口的实现类对象)
 * 	*后置增强:setAfterAdvice(AfterAdvice接口的实现类对象)
 * 3.调用createProxy()得到代理对象
 * 
 * 	注意:在执行代理对象方法时
 * 		>执行BeforeAdvice的before()
 * 		>执行目标对象的目标方法
 * 		>执行AfterAdvice的after()
 *
 */
public class ProxyFactory {
	private Object targetObject;//目标对象
	private BeforeAdvice beforeAdvice;//前置增强
	private AfterAdvice afterAdvice;//后置增强
	
	/**
	 * 用来生成代理对象
	 * 
	 * @return
	 */
	public Object createProxy() {
		ClassLoader loader = this.getClass().getClassLoader();
		Class[] interfaces = targetObject.getClass().getInterfaces();//获取目标对象的类型,然后获取实现的所有接口类型
		InvocationHandler h = new InvocationHandler() {
			public Object invoke(Object proxy, Method method, Object[] args)
					throws Throwable {
				/*
				 * 在调用代理对象的方法时会执行这里的内容
				 */
				//执行前置增强
				if (beforeAdvice != null) {
					beforeAdvice.before();
				}
				//执行目标对象的目标方法
				Object result = method.invoke(targetObject, args);
				//执行后置增强
				if (afterAdvice != null) {
					afterAdvice.after();
				}
				//返回目标对象的返回值
				return result;
			}
		};
		/*
		 * 2.得到代理对象
		 */
		Object proxyObject = Proxy.newProxyInstance(loader, interfaces, h);
		return proxyObject;
	}
	public Object getTargetObject() {
		return targetObject;
	}
	public void setTargetObject(Object targetObject) {
		this.targetObject = targetObject;
	}
	public BeforeAdvice getBeforeAdvice() {
		return beforeAdvice;
	}
	public void setBeforeAdvice(BeforeAdvice beforeAdvice) {
		this.beforeAdvice = beforeAdvice;
	}
	public AfterAdvice getAfterAdvice() {
		return afterAdvice;
	}
	public void setAfterAdvice(AfterAdvice afterAdvice) {
		this.afterAdvice = afterAdvice;
	}
}

测试

package cn.itcast.demo3;

import org.junit.Test;
/**
 * 测试自己写的ProxyFactory
 * @author WenHuagang
 *
 */
public class Demo1 {
	@Test
	public void fun1() {
		/*
		 * 1.创建代理工厂
		 */
		ProxyFactory factory = new ProxyFactory();
		/*
		 * 2.设置目标对象
		 */
		factory.setTargetObject(new ManWaiter());
		/*
		 * 3.设置前置增强对象
		 */
		factory.setBeforeAdvice(new BeforeAdvice() {
			
			public void before() {
				System.out.println("你好!");
			}
		});
		/*
		 * 4.设置后置增强对象
		 */
		factory.setAfterAdvice(new AfterAdvice() {
			
			public void after() {
				System.out.println("再见!");
			}
		});
		
		/*
		 * 5.得到代理对象
		 * 这个代理对象(Object)的类实现了所有目标对象(ManWaiter)实现的接口(Waiter)
		 */
		 Waiter waiterProxy = (Waiter) factory.createProxy();//实际返回Object o
		 
		 waiterProxy.serve();
		
	}

}

动态代理之代理工厂实现_第1张图片

你可能感兴趣的:(Java)