java 映射函数,如何将字符串映射到Java中的函数?

Currently, I have a bunch of Java classes that implement a Processor interface, meaning they all have a processRequest(String key) method. The idea is that each class has a few (say, <10) member Strings, and each of those maps to a method in that class via the processRequest method, like so:

class FooProcessor implements Processor

{

String key1 = "abc";

String key2 = "def";

String key3 = "ghi";

// and so on...

String processRequest(String key)

{

String toReturn = null;

if (key1.equals(key)) toReturn = method1();

else if (key2.equals(key)) toReturn = method2();

else if (key3.equals(key)) toReturn = method3();

// and so on...

return toReturn;

}

String method1() { // do stuff }

String method2() { // do other stuff }

String method3() { // do other other stuff }

// and so on...

}

You get the idea.

This was working fine for me, but now I need a runtime-accessible mapping from key to function; not every function actually returns a String (some return void) and I need to dynamically access the return type (using reflection) of each function in each class that there's a key for. I already have a manager that knows about all the keys, but not the mapping from key to function.

My first instinct was to replace this mapping using if-else statements with a Map, like I could do in Javascript. But, Java doesn't support first-class functions so I'm out of luck there. I could probably dig up a third-party library that lets me work with first-class functions, but I haven't seen any yet, and I doubt that I need an entire new library.

I also thought of putting these String keys into an array and using reflection to invoke the methods by name, but I see two downsides to this method:

My keys would have to be named the same as the method - or be named in a particular, consistent way so that it's easy to map them to the method name.

This seems WAY slower than the if-else statements I have right now. Efficiency is something of a concern because these methods will tend to get called pretty frequently, and I want to minimize unnecessary overhead.

TL; DR: I'm looking for a clean, minimal-overhead way to map a String to some sort of a Function object that I can invoke and call (something like) getReturnType() on. I don't especially mind using a 3rd-party library if it really fits my needs. I also don't mind using reflection, though I would strongly prefer to avoid using reflection every single time I do a method lookup - maybe using some caching strategy that combines the Map with reflection.

Thoughts on a good way to get what I want? Cheers!

解决方案

Couldn't you do String to Method? Then you can cache the methods you need to execute.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值