设计模式----代理模式

代理模式定义

引用百度百科:为其他对象提供一种代理以控制对这个对象的访问。在某些情况下,一个对象不适合或者不能直接引用另一个对象,而代理对象可以在客户端和目标对象之间起到中介的作用。

静态代理

基于接口的静态代理

定义接口

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();
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值