日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java 委托机制_通过反射实现Java下的委托机制代码详解

發(fā)布時間:2024/9/15 java 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 委托机制_通过反射实现Java下的委托机制代码详解 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

簡述

一直對Java沒有現(xiàn)成的委托機制耿耿于懷,所幸最近有點時間,用反射寫了一個簡單的委托模塊,以供參考。

模塊API

public Class Delegater()//空參構(gòu)造,該類管理委托實例并實現(xiàn)委托方法

//添加一個靜態(tài)方法委托,返回整型值ID代表該方法與參數(shù)構(gòu)成的實例。若失敗,則返回-1。

public synchronized int addFunctionDelegate(Class> srcClass,String methodName,Object... params);

//添加一個實例方法委托,返回整型值ID代表該方法與參數(shù)構(gòu)成的實例。若失敗,則返回-1。

public synchronized int addFunctionDelegate(Object srcObj,String methodName,Object... params);

//根據(jù)整型ID從委托實例中刪除一個方法委托,返回是否成功

public synchronized Boolean removeMethod(int registerID);

//依次執(zhí)行該委托實例中的所有方法委托(無序)

public synchronized void invokeAllMethod();

//將參數(shù)表轉(zhuǎn)換為參數(shù)類型表

private Class>[] getParamTypes(Object[] params);

//由指定的Class、方法名、參數(shù)類型表獲得方法實例

private Method getDstMethod(Class> srcClass,String methodName,Class>[] paramTypes);

class DelegateNode(Method refMethod,Object[] params)//DelegateNode類在不使用Object構(gòu)造時敘述了一個靜態(tài)方法委托,包括方法實例及參數(shù)表

class DelegateNode(Object srcObj,Method refMethod,Object[] params)//DelegateNode類在使用Object構(gòu)造時敘述了一個實例方法委托,包括類實例、方法實例及參數(shù)表

public void invokeMethod();

//執(zhí)行該節(jié)點敘述的方法委托

源代碼

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

import java.util.Hashtable;

/**Delegater類使用RTTI及反射實現(xiàn)Java下的委托機制

* @author 三向板磚

* */

public class Delegater {

static int register = Integer.MIN_VALUE;

//ID分配變量

Hashtable nodeTable;

//管理ID與對應(yīng)委托的容器

public Delegater()

{

nodeTable = new Hashtable();

}

//添加靜態(tài)方法委托

public synchronized int addFunctionDelegate(Class> srcClass,String methodName,Object... params)

{

Class>[] paramTypes = getParamTypes(params);

Method refMethod;

if((refMethod = getDstMethod(srcClass,methodName,paramTypes)) != null)

{

register++;

nodeTable.put(register,new DelegateNode(refMethod, params));

return register;

} else

{

return -1;

}

}

//添加動態(tài)方法委托

public synchronized int addFunctionDelegate(Object srcObj,String methodName,Object... params)

{

Class>[] paramTypes = getParamTypes(params);

Method refMethod;

if((refMethod = getDstMethod(srcObj.getClass(),methodName,paramTypes)) != null)

{

register++;

nodeTable.put(register,new DelegateNode(srcObj,refMethod, params));

return register;

} else

{

return -1;

}

}

//刪除一個方法委托

public synchronized Boolean removeMethod(int registerID)

{

if(nodeTable.containsKey(registerID))

{

nodeTable.remove(registerID);

return true;

}

return false;

}

//無序地執(zhí)行委托方法

public synchronized void invokeAllMethod()

{

for (DelegateNode node:nodeTable.values())

{

node.invokeMethod();

}

}

//將參數(shù)表轉(zhuǎn)化為參數(shù)類型表

private Class>[] getParamTypes(Object[] params)

{

Class>[] paramTypes = new Class>[params.length];

for (int i = 0;i < params.length;i++)

{

paramTypes[i] = params[i].getClass();

}

return paramTypes;

}

//根據(jù)Class類實例、方法名、參數(shù)類型表獲得一個Method實例

private Method getDstMethod(Class> srcClass,String methodName,Class>[] paramTypes)

{

Method result = null;

try {

result = srcClass.getMethod(methodName, paramTypes);

if(result.getReturnType() != void.class)

{

System.out.println("Warning,Method:"+methodName+" has a return value!");

}

}

catch (NoSuchMethodException | SecurityException e) {

System.out.println("Can Not Found Method:"+methodName+",ensure it's exist and visible!");

}

return result;

}

}

class DelegateNode

{

Object srcObj;

Method refMethod;

Object[] params;

public DelegateNode(Method refMethod,Object[] params)

{

this.refMethod = refMethod;

this.params = params;

}

public DelegateNode(Object srcObj,Method refMethod,Object[] params)

{

this.srcObj = srcObj;

this.refMethod = refMethod;

this.params = params;

}

public void invokeMethod()

{

try {

refMethod.invoke(srcObj,params);

}

catch (IllegalAccessException | IllegalArgumentException

| InvocationTargetException e) {

System.out.println("Method:"+refMethod.toString()+" invoke fail!");

}

}

}

模塊測試

public class DelegaterTest {

public void showInfo()

{

System.out.println("Hello Delegate!");

}

public void showCustomInfo(String info)

{

System.out.println(info);

}

public static void showStaticInfo()

{

System.out.println("Static Delegate!");

}

public static void showCustomStaticInfo(String info)

{

System.out.println(info);

}

public static void main(String[] args) {

Delegater dele = new Delegater();

DelegaterTest tester = new DelegaterTest();

int ID = dele.addFunctionDelegate(tester,"showInfo");

dele.addFunctionDelegate(tester,"showCustomInfo","Custom!");

dele.addFunctionDelegate(DelegaterTest.class,"showStaticInfo");

dele.addFunctionDelegate(DelegaterTest.class,"showCustomStaticInfo","StaticCustom!");

dele.invokeAllMethod();

dele.removeMethod(ID);

System.out.println("------------------");

dele.invokeAllMethod();

}

}

執(zhí)行結(jié)果:

StaticCustom!

StaticDelegate!

Custom!

HelloDelegate!

------------------

StaticCustom!

StaticDelegate!

Custom!

其他事項

一些public方法使用synchronized是為了保證register變量的線程安全,使其不會因為多線程而出錯。

對于有返回值的委托,會報出警告,但模塊還是接受這樣的委托的,不過在執(zhí)行委托時您將不能得到返回值。

添加的委托最大值是Integer.MAX_VALUE-Integer.MIN_VALUE超出后的容錯處理沒有考慮(一般也沒這么多函數(shù)需要委托的吧。

委托執(zhí)行是無序的,而且,需要性能要求時,委托的函數(shù)盡量不要有阻塞過程,否則會影響其他委托函數(shù)的執(zhí)行。

還有什么問題可以發(fā)上來一同探討。

總結(jié)

以上就是本文關(guān)于通過反射實現(xiàn)Java下的委托機制代碼詳解的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他Java相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!

總結(jié)

以上是生活随笔為你收集整理的java 委托机制_通过反射实现Java下的委托机制代码详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。