什么是代理模式香追?
為其他對象提供一種代理以控制對這個對象的訪問悔政√阑眨——《Java設(shè)計模式》
■ 抽象主題(Subject)角色:該角色是真實主題和代理主題的共同接口涵叮,以便在任何可以使用真實主題的地方都可以使用代理主題。
■ 代理主題(Proxy Subject)角色:也叫做委托類熙含、代理類罚缕,該角色
1)負責(zé)控制對真實主題的引用,
2)負責(zé)在需要的時候創(chuàng)建或刪除真實主題對象
3)在真實主題角色處理完畢前后做預(yù)處理和善后處理工作怎静。
■ 真實主題(Real Subject)角色:該角色也叫做被委托角色邮弹、被代理角色,是業(yè)務(wù)邏輯的具體執(zhí)行者蚓聘。
1.靜態(tài)代理
Java中的靜態(tài)代理要求代理類(ProxySubject)和委托類(RealSubject)都實現(xiàn)同一個接口(Subject)腌乡。
1)靜態(tài)代理中代理類在編譯期就已經(jīng)確定。
2)靜態(tài)代理的效率相對動態(tài)代理來說相對高一些
3)靜態(tài)代理代碼冗余大夜牡,一單需要修改接口导饲,代理類和委托類都需要修改。
舉例:
接口 Developer:
public interface Developer {
void coding();
void debug();
}
委托類 AndroidDeveloper:
public class AndroidDeveloper implements Developer{
private String name;
public AndroidDeveloper(String name){
this.name = name;
}
public String getDeveloperName(){
return name;
}
@Override
public void coding(){
System.out.println("Coding: "+this.name+" is developing Android!");
}
@Override
public void debug(){
System.out.println("Coding: "+this.name+" is debugging his APP!");
}
}
靜態(tài)代理類 JavaStaticProxy
public class JavaStaticProxy implements Developer{
private Developer target;
public JavaStaticProxy(Developer developer){
this.target = developer;
}
@Override
public void coding() {
System.out.println("before!");
this.target.coding();
System.out.println("after!");
}
@Override
public void debug() {
System.out.println("before!");
this.target.debug();
System.out.println("after!");
}
}
測試:
public class Main {
public static void main(String[] args){
StaticProxyTest();
}
public static void StaticProxyTest(){
System.out.println("StaticProxy:");
Developer Breeze = new AndroidDeveloper("Breeze");
JavaStaticProxy staticProxy = new JavaStaticProxy(Breeze);
staticProxy.coding();
staticProxy.debug();
}
}
輸出:
StaticProxy:
before!
Coding: Breeze is developing Android!
after!
before!
Coding: Breeze is debugging his APP!
after!
2.動態(tài)代理
Java中的動態(tài)代理依靠反射來實現(xiàn)氯材,代理類和委托類不需要實現(xiàn)同一個接口。委托類需要實現(xiàn)接口硝岗,否則無法創(chuàng)建動態(tài)代理氢哮。代理類在JVM運行時動態(tài)生成,而不是編譯期就能確定型檀。
Java動態(tài)代理主要涉及到兩個類:java.lang.reflect.Proxy和java.lang.reflect.InvocationHandler冗尤。代理類需要實現(xiàn)InvocationHandler接口或者創(chuàng)建匿名內(nèi)部類,而Proxy用于創(chuàng)建動態(tài)動態(tài)胀溺。
特點:
1)不要求代理類與委托類實現(xiàn)統(tǒng)一接口裂七,避免了靜態(tài)代理生成大量代理類的問題,但比較消耗性能仓坞。
2)可以非常靈活地在某個類背零,某個方法,某個代碼點上切入我們想要的內(nèi)容无埃,就是動態(tài)代理其中的內(nèi)容徙瓶。
2.1 例子:
動態(tài)代理類 JavaDynamicProxy
package java_proxy;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
public class JavaDynamicProxy {
private Developer target;
public JavaDynamicProxy(Developer target){
this.target = target;
}
public Object getDeveloperProxy(){
Developer developerProxy = (Developer) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().
getInterfaces(), (proxy, method, args1) -> {
Field field = target.getClass().getDeclaredField("name");
field.setAccessible(true);
String name = (String) field.get(target);
if(method.getName().equals("coding")){
beforeCoding(name);
method.invoke(target, args1);
afterCoding(name);
}
if(method.getName().equals("debug")){
beforeDebug(name);
method.invoke(target, args1);
afterDebug(name);
}
return null;
});
return developerProxy;
}
public void beforeCoding(String name){
System.out.println("Before coding: "+name+" is very happy!");
}
public void afterCoding(String name){
System.out.println("After coding: "+name+" is very tired!");
}
public void beforeDebug(String name){
System.out.println("Before debugging: "+name+"'s App has no bug! Nobody knows Android better than me!");
}
public void afterDebug(String name){
System.out.println("After debugging: What trash is "+name+"?");
}
}
測試:
package java_proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class Main {
public static void main(String[] args){
DynamicProxyTest();
}
public static void DynamicProxyTest(){
System.out.println("DynamicProxy:");
AndroidDeveloper target = new AndroidDeveloper("Breeze");
Developer proxy = (Developer)new JavaDynamicProxy(target).getDeveloperProxy();
proxy.coding();
proxy.debug();
}
}
輸出:
DynamicProxy:
Before coding: Breeze is very happy!
Coding: Breeze is developing Android!
After coding: Breeze is very tired!
Before debugging: Breeze's App has no bug! Nobody knows Android better than me!
Coding: Breeze is debugging his APP!
After debugging: What trash is Breeze?
2.2 動態(tài)代理的原理
Proxy.newProxyInstance()
把接口復(fù)制出來毛雇,通過這些接口和類加載器,拿到這個代理類cl侦镇。然后通過反射的技術(shù)復(fù)制拿到代理類的構(gòu)造函數(shù)(這部分代碼在Class類中的getConstructor0方法)灵疮,最后通過這個構(gòu)造函數(shù)new個一對象出來,同時用InvocationHandler綁定這個對象壳繁。
/* @param loader the class loader to define the proxy class
* @param interfaces the list of interfaces for the proxy class
* to implement
* @param h the invocation handler to dispatch method invocations to
* @return a proxy instance with the specified invocation handler of a
* proxy class that is defined by the specified class loader
* and that implements the specified interfaces
* @throws IllegalArgumentException if any of the <a href="#restrictions">
* restrictions</a> on the parameters are violated
* @throws SecurityException if a security manager, <em>s</em>, is present
* and any of the following conditions is met:
* <ul>
* <li> the given {@code loader} is {@code null} and
* the caller's class loader is not {@code null} and the
* invocation of {@link SecurityManager#checkPermission
* s.checkPermission} with
* {@code RuntimePermission("getClassLoader")} permission
* denies access;</li>
* <li> for each proxy interface, {@code intf},
* the caller's class loader is not the same as or an
* ancestor of the class loader for {@code intf} and
* invocation of {@link SecurityManager#checkPackageAccess
* s.checkPackageAccess()} denies access to {@code intf};</li>
* <li> any of the given proxy interfaces is non-public and the
* caller class is not in the same {@linkplain Package runtime package}
* as the non-public interface and the invocation of
* {@link SecurityManager#checkPermission s.checkPermission} with
* {@code ReflectPermission("newProxyInPackage.{package name}")}
* permission denies access.</li>
* </ul>
* @throws NullPointerException if the {@code interfaces} array
* argument or any of its elements are {@code null}, or
* if the invocation handler, {@code h}, is
* {@code null}
*
* @see <a href="#membership">Package and Module Membership of Proxy Class</a>
* @revised 9
* @spec JPMS
*/
@CallerSensitive
public static Object newProxyInstance(ClassLoader loader,
Class<?>[] interfaces,
InvocationHandler h) {
Objects.requireNonNull(h);
final Class<?> caller = System.getSecurityManager() == null
? null
: Reflection.getCallerClass();
/*
* Look up or generate the designated proxy class and its constructor.
*/
Constructor<?> cons = getProxyConstructor(caller, loader, interfaces);
return newProxyInstance(caller, cons, h);
}
InvocationHandler
InvocationHandler作用就是震捣,當(dāng)代理對象的原本方法被調(diào)用的時候,會綁定執(zhí)行一個方法闹炉,這個方法就是InvocationHandler里面定義的內(nèi)容蒿赢,同時會替代原本方法的結(jié)果返回。
InvocationHandler接收三個參數(shù)
- proxy剩胁,代理后的實例對象诉植。
- method,對象被調(diào)用方法昵观。
- args晾腔,調(diào)用時的參數(shù)。
public interface InvocationHandler {
/**
* Processes a method invocation on a proxy instance and returns
* the result. This method will be invoked on an invocation handler
* when a method is invoked on a proxy instance that it is
* associated with.
*
* @param proxy the proxy instance that the method was invoked on
*
* @param method the {@code Method} instance corresponding to
* the interface method invoked on the proxy instance. The declaring
* class of the {@code Method} object will be the interface that
* the method was declared in, which may be a superinterface of the
* proxy interface that the proxy class inherits the method through.
*
* @param args an array of objects containing the values of the
* arguments passed in the method invocation on the proxy instance,
* or {@code null} if interface method takes no arguments.
* Arguments of primitive types are wrapped in instances of the
* appropriate primitive wrapper class, such as
* {@code java.lang.Integer} or {@code java.lang.Boolean}.
*
* @return the value to return from the method invocation on the
* proxy instance. If the declared return type of the interface
* method is a primitive type, then the value returned by
* this method must be an instance of the corresponding primitive
* wrapper class; otherwise, it must be a type assignable to the
* declared return type. If the value returned by this method is
* {@code null} and the interface method's return type is
* primitive, then a {@code NullPointerException} will be
* thrown by the method invocation on the proxy instance. If the
* value returned by this method is otherwise not compatible with
* the interface method's declared return type as described above,
* a {@code ClassCastException} will be thrown by the method
* invocation on the proxy instance.
……
*/
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable;
}