博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java框架基础 静态代理和动态代理
阅读量:2443 次
发布时间:2019-05-10

本文共 2844 字,大约阅读时间需要 9 分钟。

首先我想说一下,类加载器和反射一个类的区别
class类加载器
xxx.getClass().getClassLoader(); 先通过反射获得,一个类,通过类加载器来加载一个类的同时创建出一个类,而getClass一个类只可以获得这个类中的方法和属性.
class用getclass;  反射获得一个类,getClass一个类只可以获得这个类中的方法和属性.

1、代理模式的分类:
代理模式的定义:为其他对象提供一种代理以控制对这个对象的访问。在某些情况下,一个对象不适合或者不能直接引用另一个对象,而代理对象可以在客户端和目标对象之间起到中介的作用。
代理分为“静态代理”和“动态代理”:静态代理需要我们自己创建代理对象(需要创建代理类),而动态代理的代理对象是自动生成的,我们不需要关系代理对象的生成过程。
2、代理模式中有“抽象角色(接口)”、“代理角色”、“真实角色(目标角色)”这三个角色,代理对象和真实对象都会去实现“抽象角色”这个接口,     也就是说代理模式是基于接口来实现的。
3、对代理对象的理解,和代码的实现

静态代理

package com.my.proxy;/* * 抽象角色(接口) */public interface IProduct {	public int product();}package com.my.proxy;//目标对象,真实对象public class LiLingFactory implements IProduct{	@Override	public int product() {		// TODO Auto-generated method stub		int product=100;		System.out.println("我要买鞋子,价格要低于"+product);		return product;	}}package com.my.proxy;//代理对象,作为直接对象的代理,静态代理public class LiLingStore implements IProduct {	@Override	public int product() {		// TODO Auto-generated method stub		LiLingFactory lf=new LiLingFactory();		int product = lf.product();		return product*3;	}}package com.my.proxy;//这种方式是静态代理public class Mytest {	public static void main(String[] args) {				//直接通过工厂,直接对象来获得		LiLingFactory lf=new LiLingFactory();		int product = lf.product();		System.out.println(product+"最低格");				//通过代理对象来获得值			LiLingStore store = new LiLingStore();		int product2 = store.product();		System.out.println(product2);	}}
动态代理

//定义一个房东和租房者相同的方法public interface HousePrice {		public int price();}目标对象package com.my.proxy01;public class HouseOwer implements HousePrice{	@Override	public int price() {		// TODO Auto-generated method stub		int price=100;		System.out.println("房价又涨了,房东又要涨房租了");		return price;			}}package com.my.proxy01;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;public class MyHouse {	public static void main(String[] args) {		/*		 * loader是类加载器,通过反射来加载类		 */				//获取直接对象(目标对象)		HouseOwer houseOwer = new HouseOwer();				//获得类加载器		ClassLoader houseOwerLoader = houseOwer.getClass().getClassLoader();		//获得对象中接口方法的一个数组		Class
[] houseOwerinterface = houseOwer.getClass().getInterfaces(); //其实newProxyInstance方法相当于动态的创建了一个类,通过传类加载器,获取中间的接口,创建出来了一个动态代理的类 HousePrice proxy = (HousePrice) Proxy.newProxyInstance(houseOwerLoader, houseOwerinterface, new InvocationHandler() { //new InvocationHandler()相当于是一个监听器来监听了直接对象 /* * proxy是获取当前直接对象 * method是获取直接对象中的方法 * args中的值 * 返回值:返回值就是代理对象代用方法的返回值 * * */ @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { // TODO Auto-generated method stub Object invoke = method.invoke(houseOwer, args); System.out.println(method); System.out.println("我一定会调用的"); return invoke; } }); //调用代理对象的方法 int price = proxy.price(); System.out.println(price); }}

动态代理中的原理,代码注释中已经讲的很清楚了,大家自己看看哈

转载地址:http://hvjqb.baihongyu.com/

你可能感兴趣的文章
很容易爱上jQuery
查看>>
SitePoint播客#50:jQuery:《忍者新手》
查看>>
Microhoo / Yahsoft合作伙伴关系不断发展
查看>>
Google发布Chrome 4.0
查看>>
将ip保存在txt文档中_将任何文件存储在Google文档中
查看>>
disconf apps_Google Apps Drop IE6支持
查看>>
centos who_Who博士的新徽标设计
查看>>
lgo软件许可号许可码_软件是许可的还是出售的?
查看>>
SitePoint播客#29:Roy Rubin访谈
查看>>
Opera 10 Final发布
查看>>
使用Screenr创建截屏视频
查看>>
RockMelt:另一天,另一个新浏览器
查看>>
SitePoint播客#26:力量在于您
查看>>
SitePoint PDF上没有更多密码!
查看>>
coldfusion_五星! 向您的ColdFusion应用程序添加评分小部件
查看>>
SitePoint Podcast#24:这些框架具有讽刺意味
查看>>
jpc2bmp_使用JPC在您的手机上运行PC应用程序
查看>>
回溯法怎么回溯_无需浏览器的Windows 7 E上的Microsoft回溯
查看>>
网页版谷歌地图上标记多个点_Google正式放弃Beta版标记
查看>>
火狐游览器下载62.0.3_Firefox 3.5已发布-立即下载!
查看>>