[原]java中利用EMethodMap进行对象间调用
也许是小弟学艺不精,便创造了"EMethodMap"来进行偷懒...
1.初始化 (可以省略)
在调用之前必须进行初始化“EMethodMapManage”对象。
调用“EMethodMapManage”静态方法:
EMethodMapManage.createEMethodMapManage();
2.注册方法
方法原型 : public Object name(EMethodMessage msg);
将被调用的方法(public)注册到”EMethodMapManage“中。
/** * 将 obj 对象的实现消息机制的方法全部注册为消息映射 * @param name 前缀名 * @param obj 对象 * @return */ EMethodMapManage.addMethodMap(String name,Object obj); /** * 添加消息映射 * @param name 触发名 * @param obj 触发目标对象 * @param methodName 触发目标方法名 * @return */ EMethodMapManage.addMethodMap(String name,Object obj,String methodName)
3.调用方法
在需要方法调用的地方执行:
/** * 发送消息 * @param name 消息触发名 * @param obj 消息触发对象 * @param parameter 参数(多参数) * @return */ EMethodMapManage.sendMethodMessage(String name,Object obj,Object[] parameter); /** * 发送消息 * @param name 消息触发名 * @param obj 消息触发对象 * @param parameter 参数 * @return */ EMethodMapManage.sendMethodMessage(String name,Object obj,Object parameter)
4.注销方法映射
不用的时候最好注销,以便java释放资源
/** * 删除name对应的消息映射 * @param name * @return */ EMethodMapManage.removeMethodMap(String name); /** * 删除所有的消息映射 * @return */ EMethodMapManage.removeAllMethodMap();
示例:
package ekaiser.nzlov.methodmap; public class Test { public Test(){ EMethodMapManage.addMethodMap("Test", this); } public void a(EMethodMessage msg){ System.out.println("调用a"); } public boolean b(EMethodMessage msg){ System.out.println("调用b"); return true; } public void c(EMethodMessage msg){ System.out.println("调用c:"+msg.getParameter()); } public static void main(String[] a){ EMethodMapManage.createEMethodMapManage(); Test t = new Test(); EMethodMapManage.sendMethodMessage("Test:a", "obj", ""); Boolean b = (Boolean)EMethodMapManage.sendMethodMessage("Test:b", "obj", ""); System.out.println("b:"+b); EMethodMapManage.sendMethodMessage("Test:c", "obj", "hello"); } }