简述
一直对Java没有现成的委托机制耿耿于怀,所幸最近有点时间,用反射写了一个简单的委托模块,以供参考。
模块API
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public Class Delegater()//空参构造,该类管理委托实例并实现委托方法
//添加一个静态方法委托,返回整型值ID代表该方法与参数构成的实例。若失败,则返回-1。
public synchronized int addFunctionDelegate(Class<?> srcClass,String methodName,Object... params);
//添加一个实例方法委托,返回整型值ID代表该方法与参数构成的实例。若失败,则返回-1。
public synchronized int addFunctionDelegate(Object srcObj,String methodName,Object... params);
//根据整型ID从委托实例中删除一个方法委托,返回是否成功
public synchronized Boolean removeMethod(int registerID);
//依次执行该委托实例中的所有方法委托(无序)
public synchronized void invokeAllMethod();
//将参数表转换为参数类型表
private Class<?>[] getParamTypes(Object[] params);
//由指定的Class、方法名、参数类型表获得方法实例
private Method getDstMethod(Class<?> srcClass,String methodName,Class<?>[] paramTypes);
class DelegateNode(Method refMethod,Object[] params)//DelegateNode类在不使用Object构造时叙述了一个静态方法委托,包括方法实例及参数表
class DelegateNode(Object srcObj,Method refMethod,Object[] params)//DelegateNode类在使用Object构造时叙述了一个实例方法委托,包括类实例、方法实例及参数表
public void invokeMethod();
//执行该节点叙述的方法委托
|
源代码
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Hashtable;
/**Delegater类使用RTTI及反射实现Java下的委托机制
* @author 三向板砖
* */
public class Delegater {
static int register = Integer.MIN_VALUE;
//ID分配变量
Hashtable<Integer,DelegateNode> nodeTable;
//管理ID与对应委托的容器
public Delegater()
{
nodeTable = new Hashtable<Integer,DelegateNode>();
}
//添加静态方法委托
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;
}
}
//添加动态方法委托
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;
}
//无序地执行委托方法
public synchronized void invokeAllMethod()
{
for (DelegateNode node:nodeTable.values())
{
node.invokeMethod();
}
}
//将参数表转化为参数类型表
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;
}
//根据Class类实例、方法名、参数类型表获得一个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!");
}
}
}
|
模块测试
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
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();
}
}
|
执行结果:
StaticCustom!
StaticDelegate!
Custom!
HelloDelegate!
——————
StaticCustom!
StaticDelegate!
Custom!
其他事项
一些public方法使用synchronized是为了保证register变量的线程安全,使其不会因为多线程而出错。
对于有返回值的委托,会报出警告,但模块还是接受这样的委托的,不过在执行委托时您将不能得到返回值。
添加的委托最大值是Integer.MAX_VALUE-Integer.MIN_VALUE超出后的容错处理没有考虑(一般也没这么多函数需要委托的吧。
委托执行是无序的,而且,需要性能要求时,委托的函数尽量不要有阻塞过程,否则会影响其他委托函数的执行。
还有什么问题可以发上来一同探讨。
总结
以上就是本文关于通过反射实现Java下的委托机制代码详解的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他Java相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
原文链接:http://blog.csdn.net/shuzhe66/article/details/26148115
相关文章
猜你喜欢
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
iOS 泛型中nullable、null resettable、null kindof 用法详解
2025-05-29 100 -
2025-06-05 41
-
2025-06-04 84
-
2025-05-29 62
-
2025-05-25 49
热门评论

