Java的設(shè)計模式之代理模式使用詳解
一、代理模式介紹
代理模式(Proxy Pattern),是23種設(shè)計模式之一,它關(guān)心的主要是過程,而不是結(jié)果。
代理模式主要提供了對目標(biāo)對象的間接訪問方式,即通過代理對象來訪問目標(biāo)對象,這樣可以在目標(biāo)對象實現(xiàn)的基礎(chǔ)上,增強額外的功能操作,即擴展目標(biāo)對象的功能,SpringAop便是一個很好的例子。
而被代理的對象可以是遠(yuǎn)程對象、創(chuàng)建開銷大的對象或需要安全控制的對象。
代理模式的簡單示意圖如下。
代理模式主要有三種實現(xiàn)方式 靜態(tài)代理、動態(tài)代理(JDK代理、接口代理)和Cglib代理(不需要實現(xiàn)接口)。
二、三種代理模式的簡單介紹
1、靜態(tài)代理
介紹
靜態(tài)代理UML類圖如下圖,StudentDaoProxy類是代理類,通過聚合的方式代理StudentDao類,同時StudentDao類和代理類都實現(xiàn)IStudentDao接口類。
靜態(tài)代理的優(yōu)點是能夠較為簡單快速的在不修改目標(biāo)對象的前提下,對目標(biāo)對象功能進(jìn)行擴展;而缺點就是不夠靈活,如果接口新增方法,那么需要維護(hù)類的成本太大。
代碼實現(xiàn)
①新建IStudentDao接口類
public interface IStudentDao { void read(); }
②被代理的學(xué)生類
public class StudentDao implements IStudentDao{ @Override public void read() { System.out.println("正在讀書中。。。。。"); } }
③創(chuàng)建代理類
public class StudentDaoProxy implements IStudentDao{ // 被代理類聚合到代理類 private StudentDao studentDao; public StudentDaoProxy(StudentDao studentDao) { // 通過構(gòu)造器傳入聚合對象,也可以通過set方法傳入 this.studentDao=studentDao; } @Override public void read() { // 增強方法,這里可以寫其他復(fù)雜業(yè)務(wù) System.out.println("打開書本。。。。。"); //被代理類原方法 studentDao.read(); } }
④用戶測試類
public class Client { public static void main(String[] args) { StudentDao studentDao = new StudentDao(); StudentDaoProxy studentDaoProxy = new StudentDaoProxy(studentDao); // 使用代理方法 studentDaoProxy.read(); } }
輸出結(jié)果
打開書本。。。。。
正在讀書中。。。。。
2、動態(tài)代理
介紹
動態(tài)代理對象不需要實現(xiàn)接口,但是目標(biāo)對象還是需要實現(xiàn)接口。
而代理對象的生成,是利用JDK的API,動態(tài)的在內(nèi)存中構(gòu)建代理對象,相比于靜態(tài)代理靈活的多,所以也叫JDK代理或接口代理。
其UML類圖如下
代碼實現(xiàn)
①新建IStudentDao接口類
public interface IStudentDao { void read(); }
②被代理的學(xué)生類
public class StudentDao implements IStudentDao{ @Override public void read() { System.out.println("正在讀書中。。。。。"); } }
③創(chuàng)建代理類
這里相比靜態(tài)代理有點小修改,JDK代理需要使用newProxyInstance方法,其完整寫法是static Object newProxyInstance(ClassLoader loader,Class<?>[] interfaces,InvocationHandlerh),該方法在java.lang.reflect.Proxy包內(nèi),通過反射機制實現(xiàn)。
public class ProxyFactory{ // 被代理類聚合到代理類 private Object target; public ProxyFactory(Object studentDao) { // 通過構(gòu)造器傳入聚合對象,也可以通過set方法傳入 this.target=studentDao; } //給目標(biāo)對象 生成一個代理對象 public Object getProxyInstance() { //說明 /* * public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) //1. ClassLoader loader : 指定當(dāng)前目標(biāo)對象使用的類加載器, 獲取加載器的方法固定 //2. Class<?>[] interfaces: 目標(biāo)對象實現(xiàn)的接口類型,使用泛型方法確認(rèn)類型 //3. InvocationHandler h : 事情處理,執(zhí)行目標(biāo)對象的方法時,會觸發(fā)事情處理器方法, 會把當(dāng)前執(zhí)行的目標(biāo)對象方法作為參數(shù)傳入 */ return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), new MyInvocationHandler()); } class MyInvocationHandler implements InvocationHandler{ @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { // 自定義業(yè)務(wù)邏輯 System.out.println("JDK代理開始~~"); //反射機制調(diào)用目標(biāo)對象的方法 Object returnVal = method.invoke(target, args); System.out.println("JDK代理提交"); return returnVal; } } }
④用戶測試類
public class Client { public static void main(String[] args) { StudentDao studentDao = new StudentDao(); ProxyFactory studentDaoProxy = new ProxyFactory(studentDao); // 使用代理方法,注意這里一定要用接口接收 IStudentDao studentDao2 = (IStudentDao) studentDaoProxy.getProxyInstance(); studentDao2.read(); } }
結(jié)果
JDK代理開始~~
正在讀書中。。。。。
JDK代理提交
3、Cglib代理
介紹
- 動態(tài)代理或靜態(tài)代理都要求實現(xiàn)一個接口,但是對于Cglib并不需要實現(xiàn)任何接口。
- Cglib代理也叫作子類代理,它是在內(nèi)存中構(gòu)建一個子類對象從而實現(xiàn)對目標(biāo)對象功能擴展,所以也可稱為動態(tài)代理。
- Cglib是一個強大的高性能的代碼生成包,它可以在運行期擴展java類與實現(xiàn)java接口。
- 它廣泛的被許多AOP框架使用,例如SpringAOP,實現(xiàn)方法攔截。
- 在AOP編程中如何選擇代理模式:
- 目標(biāo)對象需要實現(xiàn)接口,用JDK代理
- 目標(biāo)對象不需要實現(xiàn)接口,用Cglib代理
- Cglib包的底層是通過使用字節(jié)碼處理框架ASM來轉(zhuǎn)換字節(jié)碼并生成新的類
代碼實現(xiàn)
使用Cglib時代理的類不能為final/static,否則不會被攔截,同時需要引入cglib相關(guān)jar包
①被代理的學(xué)生類
public class StudentDao implements IStudentDao{ @Override public void read() { System.out.println("正在讀書中。。。。。"); } }
②創(chuàng)建代理類
public class ProxyFactory implements MethodInterceptor{ //維護(hù)一個目標(biāo)對象 private Object target; //構(gòu)造器,傳入一個被代理的對象 public ProxyFactory(Object target) { this.target = target; } //返回一個代理對象: 是 target 對象的代理對象 public Object getProxyInstance() { //1. 創(chuàng)建一個工具類 Enhancer enhancer = new Enhancer(); //2. 設(shè)置父類 enhancer.setSuperclass(target.getClass()); //3. 設(shè)置回調(diào)函數(shù) enhancer.setCallback(this); //4. 創(chuàng)建子類對象,即代理對象 return enhancer.create(); } //重寫 intercept 方法,會調(diào)用目標(biāo)對象的方法 @Override public Object intercept(Object arg0, Method method, Object[] args, MethodProxy arg3) throws Throwable { // TODO Auto-generated method stub System.out.println("Cglib代理模式 ~~ 開始"); Object returnVal = method.invoke(target, args); System.out.println("Cglib代理模式 ~~ 提交"); return returnVal; } }
③用戶測試類
public class Client { public static void main(String[] args) { StudentDao studentDao = new StudentDao(); ProxyFactory studentDaoProxy = new ProxyFactory(studentDao); StudentDao studentDao2 = (StudentDao)studentDaoProxy.getProxyInstance(); studentDao2.read(); } }
結(jié)果
Cglib代理模式 ~~ 開始
正在讀書中。。。。。
Cglib代理模式 ~~ 提交
到此這篇關(guān)于Java的設(shè)計模式之代理模式使用詳解的文章就介紹到這了,更多相關(guān)Java代理模式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于java中構(gòu)造函數(shù)的一些知識詳解
下面小編就為大家?guī)硪黄P(guān)于java中構(gòu)造函數(shù)的一些知識詳解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12java設(shè)計模式之實現(xiàn)對象池模式示例分享
對象池模式經(jīng)常用在頻繁創(chuàng)建、銷毀對象(并且對象創(chuàng)建、銷毀開銷很大)的場景,比如數(shù)據(jù)庫連接池、線程池、任務(wù)隊列池等。本代碼簡單,沒有限制對象池大小2014-02-02Java?Socket編程從零到實戰(zhàn)詳解(完整實戰(zhàn)案例)
這篇文章主要介紹了Java?Socket編程從零到實戰(zhàn)詳解,本文給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2025-04-04Spring Boot 之HelloWorld開發(fā)案例
這篇文章主要介紹了Spring Boot 之HelloWorld開發(fā)案例,需要的朋友可以參考下2017-04-04