代理模式定义
引用百度百科:为其他对象提供一种代理以控制对这个对象的访问。在某些情况下,一个对象不适合或者不能直接引用另一个对象,而代理对象可以在客户端和目标对象之间起到中介的作用。
静态代理
基于接口的静态代理
定义接口
package a;
public interface BookService {
void add();
void test(int a,int b);
}
定义实现类
package a;
public class StudentBook implements BookService{
@Override
public void add() {
System.out.println("去书店买书");
}
@Override
public void test(int a,int b) {
System.out.println("test01-----"+a+b);
}
}
定义代理类
package a;
public class DeliverBook implements BookService{
public BookService bookservice;
public DeliverBook(BookService bookService) {
this.bookservice=bookService;
}
@Override
public void add() {
//原有逻辑
bookservice.add();
//增强逻辑
System.out.println("网上买书,快递配送");
}
@Override
public void test(int a,int b) {
// TODO Auto-generated method stub
}
}
定义测试类
package test;
import org.junit.Test;
import a.BookService;
import a.DeliverBook;
import a.StudentBook;
public class TestProxy {
@Test
public void test01() {
BookService bs=new DeliverBook(new StudentBook());
bs.add();
}
}
基于继承的静态代理
定义目标类
package a;
public class StudentBook {
public void add() {
System.out.println("去书店买书");
}
public void test(int a,int b) {
System.out.println("test01-----"+a+b);
}
}
定义代理类
package a;
import a.StudentBook;
public class DeliverBook extends StudentBook{
@Override
public void test(int a, int b) {
super.test(a, b);
System.out.println("增强逻辑...");
}
@Override
public void add() {
// TODO Auto-generated method stub
super.add();
}
}
定义测试类
package test;
import org.junit.Test;
import a.DeliverBook;
import a.StudentBook;
public class TestProxy1{
@Test
public void test01() {
StudentBook sb=new DeliverBook();
sb.test(3, 5);
}
}
动态代理
基于JDK的动态代理
定义接口
package a;
public interface BookService {
void add();
void test(int a,int b);
}
定义目标类
package a;
public class StudentBook implements BookService{
@Override
public void add() {
System.out.println("去书店买书");
}
@Override
public void test(int a,int b) {
System.out.println("test01-----"+a+b);
}
}
使用代理对象增强
package test;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import org.junit.Test;
import a.BookService;
import a.StudentBook;
/**
* 基于JDK的动态代理
* @author fusheng-fate
*
*/
public class DynamicProxyJDK {
@Test
public void test01() {
StudentBook sb = new StudentBook();
//此处返回的是一个代理类对象。通过反射机制获取
BookService bookService = (BookService)Proxy.newProxyInstance(sb.getClass().getClassLoader(), sb.getClass().getInterfaces(), new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//对特定的方法进行增强 add
Object invoke=null;
if("add".equals(method.getName())) {
System.out.println("-------------------------------");
invoke= method.invoke(sb, args);
System.out.println("此处为增强逻辑...");
}else {
invoke = method.invoke(sb, args);
}
return invoke;
}
});
bookService.test(3,5);
bookService.add();
}
}
基于cglib的动态代理
需要引入cglib库的包
定义目标类
package a;
public class StudentBook_01 {
public void add() {
System.out.println("去书店买书");
}
public void test(int a,int b) {
System.out.println("test01-----"+a+b);
}
}
生成代理对象增强
package test;
import java.lang.reflect.Method;
import org.junit.Test;
import a.StudentBook_01;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
/**
* 基于cglib的动态代理 Enhancer.create(class type,callback),
* 接口里没有方法,但是该接口的子接口MethodInterceptor有一个intercept方法
*
*/
public class DynamicProxyCglib {
@Test
public void test() {
StudentBook_01 sb=new StudentBook_01();
StudentBook_01 sbsun=(StudentBook_01)Enhancer.create(sb.getClass(),new MethodInterceptor() {
@Override
public Object intercept(Object arg0, Method method, Object[] arg2, MethodProxy arg3) throws Throwable {
//对特定的方法进行增强 add
Object invoke=null;
if("add".equals(method.getName())) {
System.out.println("-------------------------------");
invoke = method.invoke(sb, arg2);
System.out.println("此处为增强逻辑...");
}else {
invoke = method.invoke(sb, arg2);
}
return invoke;
}
});
sbsun.test(3, 5);
sbsun.add();
}
}