国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
Java代理模式復(fù)習(xí)

Java代理模式復(fù)習(xí)


1.代理模式:為一個對象提供一個替身或者占位符以控制對這個對象的訪問


@Subject接口

package com.flynewton.proxy.test;

/**
* TODO Comment of Subject
* @author flynewton
*
*/
public interface Subject {
void operation(String arg);
}

@RealSubject類

package com.flynewton.proxy.test;

/**
* TODO Comment of RealSubject
* @author flynewton
*
*/
public class RealSubject implements Subject {

/* (non-Javadoc)
* @see com.flynewton.proxy.test.Subject#operation(java.lang.String)
*/
@Override
public void operation(String arg) {
System.out.println("實際操作, 參數(shù):" + arg);
}

}

@ProxySubject類

package com.flynewton.proxy.test;

/**
* TODO Comment of ProxySubject
* @author flynewton
*
*/
public class ProxySubject implements Subject {

private Subject beProxy;

public ProxySubject(Subject beProxy) {
this.beProxy = beProxy;
}

/* (non-Javadoc)
* @see com.flynewton.proxy.test.Subject#operation(java.lang.String)
*/
@Override
public void operation(String arg) {
System.out.println("代理操作, 參數(shù):" + arg);
beProxy.operation(arg);
}

}

@測試類:

package com.flynewton.proxy.test;

/**
* TODO Comment of TestProxy
* @author flynewton
*
*/
public class TestProxy {

public static void main(String[] args) {
RealSubject realSubject = new RealSubject();
ProxySubject proxySubject = new ProxySubject(realSubject);

System.out.println("===Without Proxy===");
doSomething(realSubject);

System.out.println("===With Proxy======");
doSomething(proxySubject);
}

public static void doSomething(Subject subject){
subject.operation("flynewton");
}
}

@測試結(jié)果:

===Without Proxy===
實際操作,參數(shù):flynewton
===With Proxy======
代理操作,參數(shù):flynewton
實際操作,參數(shù):flynewton

 

2.Java動態(tài)代理

Java動態(tài)代理類位于Java.lang.reflect包下,一般主要涉及到以下兩個類:

(1).Interface InvocationHandler:該接口中僅定義了一個方法Object:invoke(Object obj,Method method, Object[] args)。在實際使用時,第一個參數(shù)obj一般是指代理類,method是被代理的方法,args為該方法的參數(shù)數(shù)組。這個抽象方法在代理類中動態(tài)實現(xiàn)。

(2).Proxy:該類即為動態(tài)代理類,作用類似于上例中的ProxySubject,其中主要包含以下內(nèi)容:

Protected Proxy(InvocationHandler h):構(gòu)造函數(shù),估計用于給內(nèi)部的h賦值。

Static Class getProxyClass (ClassLoader loader, Class[] interfaces):獲得一個代理類,其中l(wèi)oader是類裝載器,interfaces是真實類所擁有的全部接口的數(shù)組。

Static Object newProxyInstance(ClassLoader loader, Class[] interfaces, InvocationHandler h):返回代理類的一個實例,返回后的代理類可以當作被代理類使用(可使用被代理類的在Subject接口中聲明過的方法)。

@ProxyHandler類

package com.flynewton.proxy.test;

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

/**
* TODO Comment of ProxyHandler
* @author flynewton
*
*/
public class ProxyHandler implements InvocationHandler {

private Object beProxy;

/**
* @param beProxy
*/
public ProxyHandler(Object beProxy) {
super();
this.beProxy = beProxy;
}

/* (non-Javadoc)
* @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
*/
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("代理類: " + proxy.getClass() + "\n被代理方法: " + method + "\n參數(shù): " + args);
return method.invoke(beProxy, args);
}

}

@測試類:

package com.flynewton.proxy.test;

import java.lang.reflect.Proxy;

/**
* TODO Comment of TestDynamicProxy
*
* @author flynewton
*/
public class TestDynamicProxy {

/**
* @param args
*/
public static void main(String[] args) {
RealSubject realSubject = new RealSubject();
Subject proxySubject = (Subject) Proxy.newProxyInstance(Subject.class.getClassLoader(),
new Class[] { Subject.class }, new ProxyHandler(realSubject));

System.out.println("===Without Proxy===");
doSomething(realSubject);

System.out.println("===With Proxy======");
doSomething(proxySubject);
}

public static void doSomething(Subject subject) {
subject.operation("flynewton");
}
}

測試結(jié)果:

===Without Proxy===
實際操作, 參數(shù):flynewton
===With Proxy======
代理類: class $Proxy0
被代理方法: public abstract void com.flynewton.proxy.test.Subject.operation(java.lang.String)
參數(shù): [Ljava.lang.Object;@1034bb5
實際操作, 參數(shù):flynewton

本文學(xué)習(xí)并修改自:http://zhangjunhd.blog.51cto.com/113473/69996

關(guān)鍵字: Java , Proxy , 代理模式 , 動態(tài)代理

(###)

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
關(guān)于java的動態(tài)代理
struts2學(xué)習(xí)筆記(8)——攔截器原理
代理模式——結(jié)構(gòu)型模式(7)
代理模式
徹底理解JAVA動態(tài)代理
動態(tài)代理proxy與CGLib的區(qū)別
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服