Modulation of adding new Strings -> Method calls.

Posted by guesswork on Stack Overflow See other posts from Stack Overflow or by guesswork
Published on 2011-02-20T14:45:18Z Indexed on 2011/02/20 15:25 UTC
Read the original article Hit count: 190

Filed under:
|

If I have a program that does the following:

if(input=='abc'){do x}
if(input=='def'){do y}

In the future, I may want to add another piece of code like so:

if(input=='ghy'){do x}

As you can see, I am adding a new 'if' statement for a different conditional BUT using the SAME function X. The code in future has potential to have lots of different IF statements (or switches) all of which are comparing a string vs a string and then performing a function. Considering the future expansion, I was wondering if there is a possible 'neater', 'modular' way of achieving the same results.

It's a shame I can't combine the String with a Method call in a hashtable (String, method) in Java. That way I could just store any new procedures inside a hashtable and grab the relevant method for that String.

Any ideas?

Thank you

© Stack Overflow or respective owner

Related posts about java

Related posts about modularization