Java - Function on the basis of a string

Associate
Joined
14 Jun 2009
Posts
668
Would prefer to avoid an unwieldly if/else block where possible. I'm something of a Java noob so bear with me here. :p

I need to execute a different function that is defined by a string (ie, if I find that the string is "ban" it should execute my banUser() function).

I know I could do this with a selection block but it seems neater to map them together as that way I can just go through the entire list of commands (and not worry about adding extra statements in the selection block when it comes to future expansion).

I've used languages like Lua which have fairly obvious interfaces for this but I can't find an equivalent in Java for mapping a string to a function.

Any help is much appreciated. :)
 
You could use the Strategy design pattern.

Basically you create your strategy interface then have some classes implement the method to handle the command so they might have an execute method. You make a concrete strategy for each command and place them all into a HashMap<String, Strategy>.

After that all you need is some code to parse your text to isolate the commands, lookup the HashMap for the instance of the Strategy that you need to execute for that command and call the method.

http://en.wikipedia.org/wiki/Strategy_design_pattern
 
You could use the Strategy design pattern.

Basically you create your strategy interface then have some classes implement the method to handle the command so they might have an execute method. You make a concrete strategy for each command and place them all into a HashMap<string, strategy="">.

After that all you need is some code to parse your text to isolate the commands, lookup the HashMap for the instance of the Strategy that you need to execute for that command and call the method.

http://en.wikipedia.org/wiki/Strategy_design_pattern
:D

Learnt a new pattern and got it working. Thanks a lot. :)
</string,>
 
And what exactly is wrong with using if/then or select blocks? KISS

Depends how many commands are in the system and how complex they are to process. He's said one of the commands is a ban, so lets say he's programming an IRC Client or Server, he's gonna have a 50 case long if statement.

The pattern lets him add or remove commands in a far more modular and extensible manner as well as helping to encapsulate each of them. In LUA functions are first class members so essentially he'll have been doing the same thing in LUA by storing his functions in a table with a named reference, only this time in Java he just needs to put it behind an interface.
 
I know you have an answer now but just for future reference what you were looking for is called reflection in java. You could have used java.lang.reflect.Method to call a method via a string.
 
Personally I consider reflection a risk in large projects.

Programatic binding on an class or interface which the majority of IDE systems will not trace over a large code base. The mismatches may occur only at runtime when a component is changed.
Usually automated testing catches it but it's a higher risk in reality than a static interface.
 
Back
Top Bottom